Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView resize with auto layout on rotation

I have some weird bug and I can't find the solution.

I have UITableView using auto layout. (Snap to the parent in all 4 sides). If I rotate device, it get rendered correctly but if I try to scroll it scroll onelly on left side of the screen.

enter image description here

The red square is scrolling area.

The next problem which I assume is connected whit this one is when I try to add UIView as subView to UITableView.

if I add :

CGRect frame = CGRectMake(0, 200, self.table.frame.size.width, 5);
UIView * mainView = [[UIView alloc]initWithFrame:frame];
mainView.backgroundColor = [UIColor redColor];
[self.table addSubview:mainView];

is ok, but if I do like :

UIView * mainView = [[UIView alloc]init];
mainView.backgroundColor = [UIColor redColor];
mainView.translatesAutoresizingMaskIntoConstraints = NO;
[self.table addSubview:mainView];

[self.table addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(200)-[mainView(20)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(_table, mainView)]];

[self.table addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mainView]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(_table, mainView)]];

getting na error :

*** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2903.23/UIView.m:8540
like image 266
Marko Zadravec Avatar asked Oct 20 '22 14:10

Marko Zadravec


2 Answers

For Autolayout turned on - ( Dont set any frames - it will give weird behavior )
Your Autolayout constraints should be as follows:


Your Autolayout is turned on

Your Autolayout is turned on


Your constraints should like this

if you turn off autolayout you dont required do any sort of change to your storybord or code

like image 144
bhavya kothari Avatar answered Oct 23 '22 03:10

bhavya kothari


Trying to set a random estimatedRowHeight and it seems to fix the bug. It's likely to be an bug from Apple. Swift version:

class MyTableView: UITableView {
    override func layoutSubviews() {
        self.estimatedRowHeight = 120
        super.layoutSubviews()
    }
}
like image 35
superarts.org Avatar answered Oct 23 '22 04:10

superarts.org