简体   繁体   English

将autoresizingMaskIntoConstraints转换为不起作用

[英]translatesAutoresizingMaskIntoConstraints doesn't work

I create a UIButton programmatically and add it to a view like this : 我以编程方式创建一个UIButton并将其添加到这样的视图中:

self.button = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 100, 30)];
[self.button setTitle:@"Test" forState:UIControlStateNormal];
self.button.translatesAutoresizingMaskIntoConstraints = YES;
[myView addSubview:self.button];

The button appears correctly, and it doesn't use its intrinsic size (which is correct because translatesAutoresizingMaskIntoConstraints is on). 该按钮正确显示,并且不使用其固有大小(这是正确的,因为translatesAutoresizingMaskIntoConstraints处于启用状态)。 However, when i print out myView.constraints, i do not see any constraint regarding the button. 但是,当我打印出myView.constraints时,我看不到有关该按钮的任何约束。 By definition, when translatesAutoresizingMaskIntoConstraints is on, constraints will be automatically generated and added to the superview, which is myView in this case. 根据定义,当translatesAutoresizingMaskIntoConstraints启用时,将自动生成约束并将其添加到超级视图(在本例中为myView)。

So why no constraints are generated here ? 那么,为什么这里没有约束呢? And why the layout system still knows how to layout the button on screen ? 为什么布局系统仍然知道如何在屏幕上布局按钮?

********Updated : i think i know the reason. ********更新 :我想我知道原因。 When a UIView doesn't have any constraint attached to it, Layout system will not come into place. 当UIView没有附加任何约束时,Layout系统将无法安装到位。 It will uses the view's frame. 它将使用视图的框架。 In this case, when we turn on translatesAutoresizingMaskIntoConstraints, the button's intrinsic constraints are not generated, hence the button does not have any constraint. 在这种情况下,当我们打开translatesAutoresizingMaskIntoConstraints时,不会生成按钮的固有约束,因此该按钮没有任何约束。 So the frame will be used. 因此将使用框架。 All UIView created from code will have translatesAutoresizingMaskIntoConstraints default to YES, so no constraint is automatically generated for the view. 从代码创建的所有UIView都将默认将transformsAutoresizingMaskIntoConstraints转换为YES,因此不会自动为视图生成约束。 The frame will be use. 框架将被使用。 That makes perfect sense for backward compatibility. 对于向后兼容,这是很有意义的。

The docs describe its behavior as follows: 该文档描述其行为如下:

If translatesAutoresizingMaskIntoConstraints = YES , the view's superview looks at the view's autoresizing mask, produces constraints that implement it, and adds those constraints to itself (the superview). 如果translatesAutoresizingMaskIntoConstraints = YES ,则视图的超级视图将查看视图的自动调整蒙版,生成实现该约束的约束,并将这些约束添加到自身(超级视图)。

Based on this description, it is expected to be able to look in the ‑[UIView constraints] array to find the translated constraints, but the generated constraints are not added there When you create view programatically (If view is added in nib then only we will get expected behaviour). 根据此描述,应该可以在[UIView约束]数组中查找转换后的约束,但是不会在此处添加生成的约束(当您以编程方式创建视图时(如果将视图添加到nib中,则只有将获得预期的行为)。

After some careful exploration with the debugger, I determined that the constraints are generated on demand during layout by the private method ‑[UIView(UIConstraintBasedLayout) _constraintsEquivalentToAutoresizingMask] . 经过调试器的仔细研究后,我确定约束是在布局期间通过专用方法‑[UIView(UIConstraintBasedLayout) _constraintsEquivalentToAutoresizingMask]

To View that Constraints create a Category of UIView 要查看约束,请创建UIView的类别

@interface UIView(TestConstriants)

- (NSArray *)_constraintsEquivalentToAutoresizingMask;

@end

Import this category to your ViewController class #import "UIView+TestConstriants.h" 将此类别导入您的ViewController类#import "UIView+TestConstriants.h"

Call This lines of code ofter adding self.button to view : 调用以下代码行,然后添加self.button进行查看

NSLog(@"constraints: %@", [self.button _constraintsEquivalentToAutoresizingMask]);

The above logs the constraints equivalent to autoResize mask. 上面记录了等同于autoResize掩码的约束。 Try testing by setting autoresizingMask to different values you will get corresponding equivalent constraints. 尝试通过将autoresizingMask设置为不同的值进行测试,您将获得相应的等效约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM