6

我已经在这里阅读了 iOS6 发行说明和 Autolayout + UIScrollview 的一些相关主题,但是我有一个奇怪的错误,我不知道如何解决。

视图的层次结构是:

- View
-- UIScrollView
---- UIImageView
---- UILabel

我通过 Interface Builder 在视图中添加了元素,启用了 Autolayout 并保留了默认约束。

我的标签有一个动态文本,它的高度是可消耗的(在 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-03-26 16:24:49.072 测试[34704:11303] 无法同时满足约束。以下列表中的至少一个约束可能是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的;(2) 找到添加了一个或多个不需要的约束的代码并修复它。(注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)(“”,“”)

将尝试通过打破约束来恢复

现在是具体的错误:如果我向下滚动,然后单击图像转到第二个视图,然后单击返回返回当前视图,滚动视图将从先前位置开始,因为它是顶部的看法。而且我将无法滚动查看视图的顶部。

抱歉,我没有足够的声誉来发布图片,但您可以在此处下载示例项目以重现该错误:https://docs.google.com/file/d/0B-sCDWMvn-KGS19sUkZnUFdWWTg/edit? usp=分享

感谢您的帮助,卢卡斯

编辑此处错误的解决方法:UIScrollView wrong offset with Auto Layout

4

1 回答 1

1

根据控制台中的消息。您来自 xib 的垂直约束与您在代码中添加的约束冲突。如果你只是想改变之间的垂直空间imageViewlabel然后在 xib 中进行。

关于你的错误。这是解决方案点击此处

于 2013-03-26T17:36:37.567 回答