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.
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
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
if you turn off autolayout you dont required do any sort of change to your storybord or code
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With