Using autolayout constraints programmatically

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 02:27:08

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!