我已經閱讀了iOS6發行說明和一些Autolayout + UIScrollview的相關主題,但我有一個奇怪的錯誤,我不知道該如何解決。Autolayout UIScrollView約束奇怪的行爲
視圖的層次是:
- View
-- UIScrollView
---- UIImageView
---- UILabel
我添加元素在通過界面生成器的視圖,其中自動佈局ON並保持默認約束。
我的標籤有一個動態文本,其高度是可消耗的(IB中配置的行數= 0)。 我希望滾動視圖自動適應圖像視圖+標籤的高度。
下面是相關的視圖控制器類代碼:
- (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];
}
正如你所看到的,我加入了滾動視圖垂直約束。這似乎工作得很好,但我在控制檯以下消息:
2013年3月26日16:24:49.072試驗[34704:11303]無法同時滿足 約束。 以下列表中的至少一個約束可能是您不想要的。試試這個:(1)看看每個約束條件,並試圖找出你不期望的; (2)找到 添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到你 不懂NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產 translatesAutoresizingMaskIntoConstraints)( 「」, 「」)
將嘗試打破約束 恢復
而現在的具體錯誤: 如果我滾動下來,然後我點擊圖片進入第二視圖,單擊返回,返回到當前視圖,滾動視圖將從開始先例的位置,因爲它是v的頂部IEW。而且我將無法滾動查看視圖。
我很抱歉,我沒有足夠的信譽發表圖片,但你可以在這裏下載示例項目重現bug:https://docs.google.com/file/d/0B-sCDWMvn-KGS19sUkZnUFdWWTg/edit?usp=sharing
感謝您的幫助, 盧卡斯
編輯 此處的bug的解決方法:UIScrollView wrong offset with Auto Layout
謝謝,這與您給出的鏈接中描述的錯誤相同。在那裏提供解決方法。 – lukas 2013-03-30 00:07:58