問題描述
我正在調整詳細視圖控制器的狀態,就在它被推送到 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模板網!