1

I've been designing my UI programmatically, utilizing a tool called iOSHat which converts PSD Layers into Swift Code, that you can (almost perfectly) copy and paste into XCode. The PSD designs have all been for iPhone 6 specifications, but I want to be able to apply these designs to the iPhone 5 or iPhone 4 for example. Although I've been using that tool, it doesn't really impact the key concept behind this question. Below is the code for a Button that I've designed on my Splash Screen...

    let loginButton = UIButton()
    loginButton.setTranslatesAutoresizingMaskIntoConstraints(false)
    loginButton.setBackgroundImage(UIImage(named: "loginButton"), forState: .Normal)
    loginButton.setTitleColor(UIColor(red: 1, green: 1, blue: 1, alpha: 1), forState: .Normal)
    loginButton.setTitle("LOGIN", forState: .Normal)
    loginButton.titleLabel!.font = UIFont(name: "Lato-Bold", size: 13.000)
    self.view.addSubview(loginButton)

    // align loginButton from the top and bottom
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-464.5-[loginButton]-142.5-|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["loginButton": loginButton]))

    // align loginButton from the left and right
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-30-[loginButton]-196-|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["loginButton": loginButton]))

    // width constraint
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[loginButton(==149)]", options: .DirectionLeadingToTrailing, metrics: nil, views: ["loginButton": loginButton]))

    // height constraint
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[loginButton(==60)]", options: .DirectionLeadingToTrailing, metrics: nil, views: ["loginButton": loginButton]))

All of this code works perfectly on my iPhone 6 when I run it on the emulator and on my test device. But when I run it on my iPhone 5 emulator, it obviously seems to crash. Anyone have any ideas on what I can do, in optimizing my Visual Constraints to make this button scalable for the different devices.

vkrishnan23
  • 127
  • 1
  • 6
  • possible duplicate of [Get variable height for a UIButton for iPhones 5/6/6+](http://stackoverflow.com/questions/28464848/get-variable-height-for-a-uibutton-for-iphones-5-6-6) – Paul Sweatte Aug 25 '15 at 16:17

0 Answers0