当contentSize更改时,UIScrollView会调整contentOffset [英] UIScrollView adjusts contentOffset when contentSize changes

查看:1326
本文介绍了当contentSize更改时,UIScrollView会调整contentOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调整详细视图控制器的状态,就在它被推到 navigationController 之前:

I am adjusting a detail view controller's state, just before it is pushed on a navigationController:

[self.detailViewController detailsForObject:someObject];
[self.navigationController pushViewController:self.detailViewController
                                     animated:YES];

DetailViewController 中,scrollView驻留。我根据传递的对象调整了哪些内容:

In the DetailViewController a scrollView resides. Which content I resize based on the passed object:

- (void)detailsForObject:(id)someObject {
    // set some textView's content here
    self.contentView.frame = <rect with new calculated size>;

    self.scrollView.contentSize = self.contentView.frame.size;
    self.scrollView.contentOffset = CGPointZero;
}

现在,这一切都有效,但scrollView会调整它的 contentOffset 。 contentOffset 将设置为最后一个contentSize与新计算的一个之间的差异。这意味着第二次打开detailsView时,详细信息将滚动到某个不需要的位置。即使我明确地将 contentOffset 设置为 CGPointZero

Now, this all works, but the scrollView adjusts it's contentOffset during the navigationController's slide-in animation. The contentOffset will be set to the difference between the last contentSize and the new calculated one. This means that the second time you open the detailsView, the details will scroll to some unwanted location. Even though I'm setting the contentOffset to CGPointZero explicitly.

我发现重置中的 contentOffset - viewWillAppear 无效。我能想到的最好的方法是重置 viewDidAppear 中的contentOffset,导致内容显着上下移动:

I found that resetting the contentOffset in - viewWillAppear has no effect. The best I could come up with is resetting the contentOffset in viewDidAppear, causing a noticeable up and down movement of the content:

- (void)viewDidAppear:(BOOL)animated {
    self.scrollView.contentOffset = CGPointZero;
}

有没有办法阻止 UIScrollView contentSize 更改后调整 contentOffset

Is there a way to prevent a UIScrollView from adjusting its contentOffset when its contentSize is changed?

推荐答案

在推送包含 UIScrollView UIViewController 时发生使用 UINavigationController

Occurs when pushing a UIViewController containing a UIScrollView using a UINavigationController.

iOS 7

解决方案1(代码)

设置 @property(nonatomic,assign)BOOL automaticAdjustsScrollViewInsets

解决方案2(故事板)

取消选中调整滚动视图插入

iOS 6

解决方案(代码)

设置 UIScrollView 的属性 contentOffset contentInset viewWillLayoutSubviews 中。示例代码:

Set the UIScrollView's property contentOffset and contentInset in viewWillLayoutSubviews. Sample code:

- (void)viewWillLayoutSubviews{
  [super viewWillLayoutSubviews];
  self.scrollView.contentOffset = CGPointZero;
  self.scrollView.contentInset = UIEdgeInsetsZero;
}

这篇关于当contentSize更改时,UIScrollView会调整contentOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆