gpt4 book ai didi

ios - 自动布局 UIScrollView 约束奇怪的行为

转载 作者:可可西里 更新时间:2023-11-01 03:59:28 25 4
gpt4 key购买 nike

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

View 的层次结构是:

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

我通过 Interface Builder 在 View 中添加元素,启用自动布局并保留默认约束。

我的标签有一个动态文本,它的高度是可扩展的(在 IB 中配置的行数 = 0)。我希望 ScrollView 自动适应 ImageView + 标签的高度。

下面是关联的 View Controller 类的代码:

- (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];
}

如您所见,我为 ScrollView 添加了垂直约束。这似乎运作良好,但我在控制台中收到以下消息:

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

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

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

感谢您的帮助,卢卡斯

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

最佳答案

根据控制台中的消息。 xib 的垂直约束与您在代码中添加的约束冲突。如果您只想更改 imageViewlabel 之间的垂直空间,请在 xib 中进行。

关于你的错误。这是解决方案 tap here .

关于ios - 自动布局 UIScrollView 约束奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15642518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com