UIScrollView adjusts contentOffset when contentSize changes(UIScrollView 在 contentSize 改变时调整 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
中有一个滚动视图.我根据传递的对象调整了哪些内容的大小:
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;
}
现在,这一切正常,但是在 navigationController 的滑入动画期间,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.
我发现在-viewWillAppear
中重置contentOffset
没有效果.我能想到的最好办法是重置 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?
推荐答案
在使用 UINavigationController
推送包含 UIScrollView
的 UIViewController
时发生.
Occurs when pushing a UIViewController
containing a UIScrollView
using a UINavigationController
.
iOS 11+
解决方案 1(Swift 代码):
scrollView.contentInsetAdjustmentBehavior = .never
解决方案 2(故事板)
iOS 7
解决方案 1(代码)
设置 @property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets
到 NO
.
解决方案 2(故事板)
取消选中调整滚动视图插图
iOS 6
解决方案(代码)
在viewWillLayoutSubviews
中设置UIScrollView
的属性contentOffset
和contentInset
.示例代码:
Set the UIScrollView
's property contentOffset
and contentInset
in viewWillLayoutSubviews
. Sample code:
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
self.scrollView.contentOffset = CGPointZero;
self.scrollView.contentInset = UIEdgeInsetsZero;
}
这篇关于UIScrollView 在 contentSize 改变时调整 contentOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIScrollView 在 contentSize 改变时调整 contentOffset
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01