Using autolayout constraints programmatically

前端 未结 2 626
粉色の甜心
粉色の甜心 2021-02-05 17:33

I\'m trying to use autolayout in my iOS app programmatically. I have a simple view controller with this init code

UIView *i         


        
相关标签:
2条回答
  • 2021-02-05 18:21

    Just for the record: the exception concerning the missing superview gets already triggered by constraintsWithVisualFormat not addConstraints

    0 讨论(0)
  • 2021-02-05 18:34

    You have three problems:

    First, you need to opt-in to layout based contraints on your both your button subviews

    [button1 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [button2 setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    

    This will get rid of the NSAutoresizingMaskLayoutConstraint errors. Now you can get onto what's going on with your layout constraints.

    Second, you have to addSubview before adding the constraints. You are calling addContraints on innerView before button1 and button2 are subviews of innerview.

    Third, you should specify (although the docs indicate it is optional) whether the layout string is vertical or horizontal. @"H:|[button1][button2(==button1)]|". You will also want a vertical constraint like @"V:[button1]|" which would align button1 to the bottom of innerview and because you have aligned baselines on the buttons, button2 will follow.

    0 讨论(0)
提交回复
热议问题