问题描述
我正在调整详细视图控制器的状态,就在它被推送到 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)