且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

子视图,支持自动布局消失

更新时间:2023-12-05 21:01:52

这是很好的做法,以了解这些日志,但如果你要使用自动布局你将不得不在这读了。大家说,这是简单,但我个人还没有找到它的简单。

It is good practice to understand those logs but if you are going to use Autolayout you are going to have to read up on this. Everyone says it is simply but I personally have not found it simple.

苹果编程指南自动版式

请阅读本指南特别是调试部分。

Please read this guide especially the debugging section.

至于如果你要添加视图,那么你需要关闭autoresizingmasks(春季和Struts)的视图一个非常非常一般规则。添加视图作为一个子视图,并给它2或3的约束。在你的情况比你想给它一个constaint,它应该有一个向左或导致空间的0至0上海华和320宽度的顶部空间的SuperView。

As a very very general rule if you are going to add a view then you need to turn off autoresizingmasks (Spring and struts) for the view. Add the view as a subview and give it 2 or 3 constraints. In your case above you would give it a constaint that it should have a left or leading space to superview of 0. A top space to superview of 0 and a width of 320.

编辑;此处是添加视图的例子;注意,您并不需要创建一个框架。约束可能会有点陌生。第一把在上海华的中心的图。第二给它的200的宽度的下一个方法是垂直约束这使所述视图在底部,并使其2高

EDIT; Here is an example of adding a view; note you do not need to create a frame. The constraints may be a little strange. The first puts the view in the centre of the superview. The second gives it a width of 200. The next method is the vertical constraint which puts the view at the bottom and makes it 2 high.

    UIView *sView = [[UIView alloc] init];
[sView setTranslatesAutoresizingMaskIntoConstraints:NO];
[superView addSubview:sView];

[superView addConstraint:[NSLayoutConstraint constraintWithItem:sView
                                                 attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:superView
                                                 attribute:NSLayoutAttributeCenterX
                                                multiplier:1.0
                                                  constant:0]];

[superView addConstraint:[NSLayoutConstraint constraintWithItem:sView
                                                             attribute:NSLayoutAttributeWidth
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:Nil
                                                             attribute:NSLayoutAttributeWidth
                                                            multiplier:1.0
                                                              constant:200]];

[superView addConstraints:[NSLayoutConstraint
                                  constraintsWithVisualFormat:@"V:[sView(2)]|"
                                  options:0
                                  metrics:nil
                                  views:NSDictionaryOfVariableBindings(sView)]];