gpt4 book ai didi

ios - UIScrollView 错误偏移与自动布局

转载 作者:IT王子 更新时间:2023-10-29 07:40:39 35 4
gpt4 key购买 nike

我有一个相当简单的 View 配置:

一个 UIViewController,在这个 UIScrollView 中有一个子 UIScrollView 和一个 UIImageView。我将 UIImageView 设置为足以突破可见区域的高度(即高于 1024pt),并将 Bottom space 设置为 superview 将我的 UIImageView 限制为固定的正值(例如 20)。

project layout

整个设置按预期工作,图像在其父级中滚动得很好。除非 ScrollView 时(如果滚动到 View 底部效果更明显),然后消失,然后再次出现(您切换到另一个 View 并返回)滚动值恢复,但内容 ScrollView 被移动到其父 View 的外部顶部。

这个不好解释,我试着画一下: visual representation of previous paragraph

如果你想测试/查看源代码(或 Storyboard,我没有编辑一行代码)。我在我的 github 上放了一个小演示:https://github.com/guillaume-algis/iOSAutoLayoutScrollView

我确实读过 iOS 6 changelog以及关于这个特定主题的解释,并认为这是第二个选项(纯自动布局)的正确实现,但在这种情况下,为什么 UIScrollView 表现如此不稳定?我错过了什么吗?

编辑:这与 #12580434 uiscrollview-autolayout-issue 完全相同.答案只是解决方法,因为任何人都找到了解决此问题的正确方法,或者这是一个 iOS 错误?

编辑 2:我找到了另一种解决方法,它将滚动位置保持在用户离开时的相同状态(这是对 12580434 接受的答案的改进):

@interface GAViewController ()

@property CGPoint tempContentOffset;

@end


@implementation GAViewController

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

self.tempContentOffset = self.mainScrollView.contentOffset;
self.scrollView.contentOffset = CGPointZero;
}

-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

self.scrollView.contentOffset = self.tempContentOffset;
}

这基本上是在viewWillAppear中保存偏移量,将其重置为原点,然后在viewDidAppear中恢复值。问题似乎发生在这两个调用之间,但我找不到它的根源。

最佳答案

是的,在纯自动布局环境中 UIScrollView 发生了一些奇怪的事情。重读iOS SDK 6.0 release notes第二十次我发现:

Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.

解决方案

将您的 subview 连接到外部 View 。换句话说,到嵌入 ScrollView 的 View 。

由于 IB 不允许我们在 imageView 和 ScrollView 子树之外的 View 之间设置约束,例如 ScrollView 的父 View ,所以我在代码中完成了。

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view removeConstraints:[self.view constraints]];
[self.scrollView removeConstraints:[self.scrollView constraints]];
[self.imageView removeConstraints:[self.imageView constraints]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_imageView(700)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_imageView(1500)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView)]];
}

还有哇!有用!

关于ios - UIScrollView 错误偏移与自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15345522/

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