Resize UITableView with auto layout while rotating

I have some kind of strange error and I can not find a solution.

I have a UITableView using auto layout. (Binding to the parent object from all sides). If I rotate the device, it will display correctly, but if I try to scroll it, scroll it to the left of the screen.

enter image description here

The red square is the scroll area.

The next problem that I assume is related to this when I try to add a UIView as a subView in a 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];

ok but if i 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)]];

get na error:

*** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2903.23/UIView.m:8540
+3
source share
4 answers

- ( - - )
Autolayout :



Your Autolayout is turned on


Your constraints should like this

, , - .

+2

RowHeight , , . , Apple. :

class MyTableView: UITableView {
    override func layoutSubviews() {
        self.estimatedRowHeight = 120
        super.layoutSubviews()
    }
}
+1

CGRect frame = CGRectMake(0, 200, self.table.bounds.size.width, 5);

CGRect frame = CGRectMake(0, 200, self.table.frame.size.width, 5);
0

, (tableView). willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) toInterf aceOrientation duration: (NSTimeInterval) duration

self.tableView.frame = self.view.bounds;

I had a similar problem with a UITableView in a container view that wasn't scrolling correctly when rotated into portrait landscape. Only a small table bracket was scrollable. After that, everything works well.

0
source

All Articles