简体   繁体   English

将AutoresizingMaskIntoConstraints和UILabel转换为

[英]translatesAutoresizingMaskIntoConstraints and UILabel

I have created this simple custom UIView 我已经创建了这个简单的自定义UIView

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        internalView = [UIView new];
        [self addSubview:internalView];
        internalView.backgroundColor = [UIColor whiteColor];

        sublabel = [UILabel new];
        sublabel.text = @"test";
        sublabel.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.4];
        sublabel.textAlignment = NSTextAlignmentCenter;
        sublabel.translatesAutoresizingMaskIntoConstraints = NO;
        [internalView addSubview:sublabel];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    [internalView setFrame:self.bounds];
    [sublabel setFrame:internalView.bounds];
}

@end

The view is used in a xib where are defined 4 constraints: 该视图用于xib中,其中定义了4个约束:

XIB

The constrains are related to the top, trailing, leading and the height. 约束与顶部,尾部,前导和高度有关。

The views hierarchy of the custom view is self -> internalView -> sublabel 自定义视图的视图层次结构是self-> internalView-> sublabel

The layoutSubviews is doing a simple thing like setting the frame of each subview with the bounds of the superview. layoutSubviews做一件简单的事情,例如用超级视图的边界设置每个子视图的框架。

I created the code above just to point out a strange behaviour i found in translatesAutoresizingMaskIntoConstraints . 我创建了上面的代码,只是为了指出我在translatesAutoresizingMaskIntoConstraints中发现的奇怪行为。

(The yellow view is the label) If the value of the property is YES the results is what I expect from the code in the layoutSubviews method (黄色视图是标签)如果属性的值为YES,则结果是我从layoutSubviews方法中的代码期望的结果

autoresizingMaskIntoConstraints =是

If it is NO, with the same layoutSubviews i got: 如果不是,则使用相同的layoutSubviews:

autoresizingMaskIntoConstraints = NO

The documentation of the translatesAutoresizingMaskIntoConstraints says: translationsAutoresizingMaskIntoConstraints的文档说:

If this is value is 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). 如果此值为YES,则视图的超级视图将查看视图的自动调整大小蒙版,生成实现它的约束,并将这些约束添加到自身(超级视图)。

In both cases the autoresizingMask is set to UIViewAutoresizingNone and there are no constraints in the constraints property of the label. 在这两种情况下, autoresizingMask都设置为UIViewAutoresizingNone,并且标签的constraints属性中没有约束。

Why with the translatesAutoresizingMaskIntoConstraints =NO the frame that i set in the layoutSubviews is not what i see on screen? 为什么使用translatesAutoresizingMaskIntoConstraints = NO,我在layoutSubviews中设置的框架不是我在屏幕上看到的?

Edit 2: 编辑2:

I expect, given the exposed scenario, to have the same results with translatesAutoresizingMaskIntoConstraints set to YES or NO. 考虑到暴露的场景,我期望将translatesAutoresizingMaskIntoConstraints设置为YES或NO来获得相同的结果。

Edit: 编辑:

I tried to swizzle the sizeToFit method to check if it is called. 我试图混淆sizeToFit方法以检查它是否被调用。 It's not. 不是。

This is happen in iOS6/7 这是在iOS6/7发生的


Update 08/08/14 更新08/08/14

After further investigation i discovered that there is a way to change the frame of the label without having autolayout playing around. 经过进一步的调查,我发现有一种方法可以更改标签的框架,而无需自动布局。

I discovered that after the first pass of layout (when is called the layoutSubviews) with the translatesAutoresizingMaskIntoConstraints=NO autolayout adds constraints for the hugging/compression of the UILabel. 我发现,在第一次通过带有transformsAutoresizingMaskIntoConstraints = NO的布局(称为layoutSubviews)之后,自动布局会为UILabel的拥抱/压缩添加约束。 The point is that for every view that implements intrinsicContentSize returning something different from UIViewNoIntrinsicMetric the autolayout adds specific constrains. 关键是,对于每个实现internalContentContentSize且返回不同于UIViewNoIntrinsicMetric的视图,自动布局都会添加特定的约束。 That is the reason behind the resizing of the label. 这就是调整标签大小的原因。

So the first thing that i did is to reimplement a subclass of the UILabel to override the intrinsicContentSize . 因此,我要做的第一件事是重新实现UILabel的子类,以覆盖nativeContentSize

After that, following the suggestions in this really good article http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html , I tried to turn of autolayout completely for the subviews involved removing [super layoutSubviews] . 之后,按照这篇非常好的文章http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html的建议,我尝试完全关闭涉及自动删除的子视图的[自动布局] ]

The goal for me was to avoid that autolayout could act on views where a was trying to apply animated transformations. 我的目标是避免自动布局可在试图应用动画变换的视图上起作用。 So if you have the same needs i hope this can help you. 因此,如果您有相同的需求,希望对您有所帮助。

This comes more from intuition of having used it than actual study, but... 这更多的是使用它的直觉而不是实际的研究,但是...

If you set translatesAutoresizingMaskIntoConstraints to YES, the system will create constraints to enforce the frame you defined for your view. 如果将translatesAutoresizingMaskIntoConstraints设置为YES,则系统将创建约束以强制执行为视图定义的框架。

If it is set to NO, no constraints will be set for the view, and as you did not set them yourself either, the view will be resized according to default behaviours. 如果将其设置为NO,则不会为视图设置任何约束,并且由于您自己也没有设置约束,因此将根据默认行为调整视图的大小。 In this case it seems to resize to the minimum content size because of the "contentHugging" values. 在这种情况下,由于“ contentHugging”值,它似乎已调整为最小内容大小。

Bottom line is, from my understanding, when auto-layout is active all views need constraints to be properly placed. 根据我的理解,最重要的是,当启用自动布局时,所有视图都需要适当放置约束。 You either set that property to YES, or set the constraints yourself. 您可以将该属性设置为YES,或者自己设置约束。 If you don't do either, results will be a bit unpredictable. 如果您都不这样做,结果将是不可预测的。


The UIViewAutoresizingNone mask is still a valid mask, with fixed dimensions (no resizing) and it will be translated to constraints. UIViewAutoresizingNone蒙版仍然是有效的蒙版,具有固定的尺寸(不调整大小),它将转换为约束。 Views can coexist without setting constraints, when you set that option to YES. 当您将该选项设置为YES时,视图可以共存而无需设置约束。

You are interpreting UIViewAutoresizingNone as meaning "no mask" when it really means "mask with no resizing". 您将UIViewAutoresizingNone解释为“无遮罩”,而实际上是“没有调整大小的遮罩”。 Sorry to disagree with you, but I think that this is the expected behaviour :) 抱歉,不同意您的意见,但我认为这是预期的行为:)

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

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