Reputation: 765
With Xcode 6 and iOS8 I have strange issue in my app. The problem is in my custom UITableViewCell which I made with xib file. On iOS8 simulator layoutSubviews method get called infinite number of times. Like it is in "while(1)" or calls itself again and again. This is layout subviews method:
- (void)layoutSubviews
{
[super layoutSubviews];
UIEdgeInsets insets = UIEdgeInsetsMake(1, 1, 1, 1);
self.contentView.frame = UIEdgeInsetsInsetRect(UIEdgeInsetsInsetRect(self.bounds, insets), insets);
NSLog(@"Layout");
}
When I remove self.contentView.frame line, everything is OK. Also if I make self.contentView.translatesAutoresizingMaskIntoConstraints = NO, recursion stops, but it breaks all my constraints. This issue happens only in iOS8, the same method works fine in iOS6 and iOS7.
Why can't I change self.contentView.frame in this method? Is there new documentation about it?
Upvotes: 3
Views: 2878
Reputation: 1650
I was seeing similar behavior in one of my projects and this is what I think is happening based on observations. In iOS7, if you are using auto-layout and then modify the frame of the view, layoutSubviews does not get called. Thus, your frame change will be applied and everything works. In iOS8, something internally changed with UIView. Now every time the frame is changed layoutSubviews will get called.
So if we look at your code, what I think is happening is that layoutSubviews gets called. You modify the frame, which triggers layoutSubviews again. When [super layoutSubviews]
is called, it re-applies your original constraints, which resets the previous frame adjustment you made. Then you change the frame again, which triggers another layoutSubviews. This repeats infinitely.
Possible solution:
Don't modify the frame, modify the constraints instead.
You can create IBOutlets to the constraints and modify them to get the new frame you want. This will make it so that when the constraints are re-applied, they are the modified ones you made and the infinite loop will not happen.
You can get more information at:
When is layoutSubviews called?
Upvotes: 4