UIScrollView 和 UINavigationController 交互
我刚刚注意到,如果我将 UIScrollView 作为 UIViewController 的根视图:
- (void)loadView
{
self.view = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
}
我会免费获得很多行为:如果状态栏、导航栏或工具栏存在且半透明,并且如果任何导航布局发生更改,则会更新属性:
[self.navigationController setNavigationBarHidden:YES animated:YES];
不幸的是,当更改滚动视图时,滚动视图也会自动更改其 contentOffset
contentsInset
,因此当导航栏消失时内容向上,当导航栏出现时内容向下。
我很喜欢这种自由行为,但我希望我的内容在导航栏来来去去时保持静止,并且我找不到在哪里可以避免 contentOffset
的更改。
编辑:
经过一些堆栈跟踪,我发现每次自动调整偏移量时都会调用一个方法:[UIScrollView(Static) _adjustContentOffsetIfNecessary]
;但由于我找不到有关此方法的任何文档,所以我不知所措。
I just noted that if I put a UIScrollView as the root view of a UIViewController:
- (void)loadView
{
self.view = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
}
I get a lot of behavior for free: The scrollView sets it's contentInset
and scrollIndicatorInsets
automatically if the status bar, navigation bar, or toolbar are present and translucent, and updates the properties if any navigation layouts change:
[self.navigationController setNavigationBarHidden:YES animated:YES];
Unfortunately the scroll view also changes it's contentOffset
automatically when changing the contentsInset
, so the content goes up when the navigation bar disappears and down when the navigation bar appears.
I like a lot of this free behavior, but I would like for my content to stay still when the navigation bar comes and goes, and I can't find where to avoid the change in contentOffset
.
Edit:
After some stack traces, I discovered a method that is being called every time the offset is automatically adjusted: [UIScrollView(Static) _adjustContentOffsetIfNecessary]
; but since I can't find any documentation on this method, I'm at a loss.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我已经找到了。 UIViewController 有一个名为“automaticallyAdjustsScrollViewInsets”的属性,该属性在显示/隐藏导航栏时更改 contentOffset。将其设置为 no 以防止 UIScrollView 发生更改。
来源(和ios6解决方案):https://stackoverflow.com/a/20325593/936957
Ok, I have found it. UIViewController has a property called 'automaticallyAdjustsScrollViewInsets', which changes the contentOffset when showing/hiding navigationBar. Set it to no to prevent changes on the UIScrollView.
Source (and ios6 solution): https://stackoverflow.com/a/20325593/936957
您是否尝试过在隐藏/显示导航栏时设置内容插入?
隐藏时像这样:
显示时这样:
AFAIK
contentInset
不应由系统更改,它会更改应用程序的布局...Have you tried setting the content insets when you hide/show the navigation bar?
Like this when hiding:
and this when showing:
AFAIK
contentInset
shouldn't be changed by the system, it would change the layout of your app...我的解决方案是在
viewDidload
中添加:。
我没有使用
[UINavigationController setNavigationBarHidden:animated:]
,而是使用:隐藏导航栏并避免 contentOffset 更改。
并记住在
viewDidDisappear
或某个适当的地方设置隐藏状态:My solution is add:
in
viewDidload
.and instead of using
[UINavigationController setNavigationBarHidden:animated:]
, I use:to hide the navigationbar and avoid contentOffset change.
And remember to set the hidden state back in
viewDidDisappear
or some place appropriate: