3

I have created this simple custom 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

The constrains are related to the top, trailing, leading and the height.

The views hierarchy of the custom view is self -> internalView -> sublabel

The layoutSubviews is doing a simple thing like setting the frame of each subview with the bounds of the superview.

I created the code above just to point out a strange behaviour i found in 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

translatesAutoresizingMaskIntoConstraints = YES

If it is NO, with the same layoutSubviews i got:

translatesAutoresizingMaskIntoConstraints = NO

The documentation of the translatesAutoresizingMaskIntoConstraints says:

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).

In both cases the autoresizingMask is set to UIViewAutoresizingNone and there are no constraints in the constraints property of the label.

Why with the translatesAutoresizingMaskIntoConstraints=NO the frame that i set in the layoutSubviews is not what i see on screen?

Edit 2:

I expect, given the exposed scenario, to have the same results with translatesAutoresizingMaskIntoConstraints set to YES or NO.

Edit:

I tried to swizzle the sizeToFit method to check if it is called. It's not.

This is happen in iOS6/7


Update 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. The point is that for every view that implements intrinsicContentSize returning something different from UIViewNoIntrinsicMetric the autolayout adds specific constrains. 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.

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].

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.

1 Answer 1

2

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.

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. In this case it seems to resize to the minimum content size because of the "contentHugging" values.

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. 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. Views can coexist without setting constraints, when you set that option to YES.

You are interpreting UIViewAutoresizingNone as meaning "no mask" when it really means "mask with no resizing". Sorry to disagree with you, but I think that this is the expected behaviour :)

5
  • Thanks from the reply. From what i know apple says that is legit to have views that are not managed by auto layout in the hierarchy: "Views that are aware of Auto Layout can coexist in a window with views that are not". In the code above i'm explicitly setting a frame of a view that has any constraints and autoresizingMask is set UIViewAutoresizingNone. I updated the post to clarify my expectations Aug 6, 2014 at 14:44
  • The UIViewAutoresizingNone mask is still a valid mask, with fixed dimensions (no resizing) and it will be translated to constraints. Views can coexist without setting constraints, when you set that option to YES.
    – Tiago Lira
    Aug 6, 2014 at 15:05
  • You are interpreting UIViewAutoresizingNone as meaning "no mask" when it really means "mask with no resizing". Sorry to disagree with you, but I think that this is the expected behaviour :)
    – Tiago Lira
    Aug 6, 2014 at 15:07
  • ok. I see. You are right on the meaning of the UIViewAutoresizingNone when translatesAutoresizingMaskIntoConstraints=YES. Thanks Aug 6, 2014 at 15:23
  • I updated the original post to adds some discovers while I was working on my custom control. Aug 8, 2014 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.