0

enter image description here

After removing the height anchor, below is the output

enter image description here

screenshot of precise constant

enter image description here

Below is the view controller design. Took the screenshot from IB.

enter image description here

I want to change the position and size of the UIButton created using Interface Builder. I am doing it programmatically using Constraints. also learning the constraints. I have written the code of constraints. Please help me.

let size = self.view.frame.size.height -
        (self.layer.frame.size.height + (self.navigationController?.navigationBar.frame.height)! + 20)  

channelShowBtn?.translatesAutoresizingMaskIntoConstraints = false
channelShowBtn!.topAnchor.constraint(equalTo:self.view.topAnchor , constant:(((20 + (navigationController?.navigationBar.frame.height)!) + self.layer.frame.height))).isActive = true
channelShowBtn!.heightAnchor.constraint(equalToConstant:size).isActive = true
channelShowBtn!.widthAnchor.constraint(equalToConstant:39).isActive = true

Position and size should change but actually it is not changing.

Raxit Pandya
  • 113
  • 10

2 Answers2

2

Button constraints

You can set the position from xib itself.

I have attached the image below there you can set width, height, leading , trailing, top and bottom constraint as you want.

Image for button Constraint

Here you can see Add New Constraints option here you can position the button as you want

Make height constraint outlet:

@IBOutlet var heightConst: NSLayoutConstraint!

Now In view did load set height as per device

if (UIDevice.current.screenType == .iPhones_5_5s_5c_SE){
        heightConst.constant = 400
    }else{
        heightConst.constant = 200
    }

Add UIDevice extension to your code (You can also find this at this link : how to check screen size of iphone 4 and iphone 5 programmatically in swift):

 extension UIDevice {
    var iPhoneX: Bool {
        return UIScreen.main.nativeBounds.height == 2436
    }
    var iPhone: Bool {
        return UIDevice.current.userInterfaceIdiom == .phone
    }
    enum ScreenType: String {
        case iPhones_4_4S = "iPhone 4 or iPhone 4S"
        case iPhones_5_5s_5c_SE = "iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"
        case iPhones_6_6s_7_8 = "iPhone 6, iPhone 6S, iPhone 7 or iPhone 8"
        case iPhones_6Plus_6sPlus_7Plus_8Plus = "iPhone 6 Plus, iPhone 6S Plus, iPhone 7 Plus or iPhone 8 Plus"
        case iPhones_X_XS = "iPhone X or iPhone XS"
        case iPhone_XR = "iPhone XR"
        case iPhone_XSMax = "iPhone XS Max"
        case unknown
    }
    var screenType: ScreenType {
        switch UIScreen.main.nativeBounds.height {
        case 960:
            return .iPhones_4_4S
        case 1136:
            return .iPhones_5_5s_5c_SE
        case 1334:
            return .iPhones_6_6s_7_8
        case 1792:
            return .iPhone_XR
        case 1920, 2208:
            return .iPhones_6Plus_6sPlus_7Plus_8Plus
        case 2436:
            return .iPhones_X_XS
        case 2688:
            return .iPhone_XSMax
        default:
            return .unknown
        }
    }
}
Mahak Mittal
  • 121
  • 1
  • 10
  • I understand Mahak but in my app, button height it 318 it looks good on iPhone SE. but on iPhone 7 Plus it looks smaller so i want to add constraints programmatically. So look better on any iPhone. – Raxit Pandya Feb 08 '19 at 05:30
  • So you can create outlet of the height constraint and set it as per your requirement – Mahak Mittal Feb 08 '19 at 05:31
  • If you want I can provide you code as well for constraint outlet and setting it in the code as per devices – Mahak Mittal Feb 08 '19 at 05:32
  • Okay Please provide me. – Raxit Pandya Feb 08 '19 at 05:33
  • I have edited the answer. please check and let me know if you still face any problem – Mahak Mittal Feb 08 '19 at 05:42
  • @IBOutlet var heightConst: NSLayoutConstraint! I have to connect this outlet to my button right ? I have never try outlet for constraints – Raxit Pandya Feb 08 '19 at 05:48
  • You need to attach this with button width constraint. When you add width constraint to your button it will be shown up in attribute inspector. From there you can connect IBOutlet to height constraint just like you are attaching button outlet – Mahak Mittal Feb 08 '19 at 05:52
  • You can also check this link for making constraint outlet: https://stackoverflow.com/questions/22086054/unable-to-make-outlet-connection-to-a-constraint-in-ib – Mahak Mittal Feb 08 '19 at 05:53
  • Mahak I have apply outlet and above code but still issue remains the same. – Raxit Pandya Feb 08 '19 at 06:25
  • The height does not get changed as per devices?? – Mahak Mittal Feb 08 '19 at 06:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188094/discussion-between-raxit-pandya-and-mahak-mittal). – Raxit Pandya Feb 08 '19 at 06:40
1

if you want to increase height proportionality according to device you can use following extension.

    extension NSLayoutConstraint {

        @IBInspectable var preciseConstant: CGFloat {
            get {
                return constant * UIScreen.main.scale
            }
            set {

                 //replace 567 with height of Device screen that you are used to initially design in story bored
                constant =  (newValue * UIScreen.main.bounds.size.height)  / 567
                //print(constant)
            }
        }
}

After writing this extension you can see the option of precise constant in attribute inspector of particular constraints.put the same constant(30) in preciseConstant.

enter image description here

run the app your component height will be increased according to device height

Bhavesh.iosDev
  • 924
  • 9
  • 27