I already read the iOS6 release notes and some of the related topics for Autolayout + UIScrollview here, but I have a strange bug I don't know how to solve.
The view's hierarchy is :
- View
-- UIScrollView
---- UIImageView
---- UILabel
I added the elements in the view through Interface Builder, with Autolayout ON and kept the default constraints.
My label has a dynamic text and its height is expendable (number of lines = 0 configured in IB). I want that the scroll view adapts automatically to the height of the image view + the label.
Here is the code for the associated view controller class:
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
self.label.translatesAutoresizingMaskIntoConstraints = NO;
self.imageView.image = [UIImage imageNamed:@"image.png"];
self.label.text = @"Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text Text text END";
[self.label sizeToFit];
NSDictionary *viewsDictionnary = NSDictionaryOfVariableBindings(scrollView, imageView, label);
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView]-20-[label]-10-|" options:0 metrics:0 views:viewsDictionnary]];
self.imageView.userInteractionEnabled = YES;
[self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]];
}
- (void)tap
{
[self.navigationController pushViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View2"] animated:YES];
}
As you can see, I'm adding a vertical constraint for the scroll view. This seems to works well but I have the following message in the console:
2013-03-26 16:24:49.072 test[34704:11303] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "" )
Will attempt to recover by breaking constraint
And now the concrete bug: If I scroll to the down, then I click on the image to go to the second view, and click back to return to the current view, the scroll view will starts from the precedent position as it was the top of the view. And I won't be able to scroll to see the up of the view.
I'm sorry I don't have enough reputation to post images, but you can download the sample project here to reproduce the bug : https://docs.google.com/file/d/0B-sCDWMvn-KGS19sUkZnUFdWWTg/edit?usp=sharing
Thanks for the help, Lukas
EDIT Workaround for the bug here: UIScrollView wrong offset with Auto Layout
According to the message in console. Your vertical constraints from xib conflict with constraints that you added in code. If you just want to change vertical space between imageView
and label
then do it in xib.
Concerning to you bug. Here is the solution tap here.
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