UIScrollView 和 UINavigationController 交互

发布于 2024-12-11 23:15:54 字数 722 浏览 0 评论 0原文

我刚刚注意到,如果我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木緿 2024-12-18 23:15:54

好的,我已经找到了。 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

妖妓 2024-12-18 23:15:54

您是否尝试过在隐藏/显示导航栏时设置内容插入?

隐藏时像这样:

[self.navigationController setNavigationBarHidden:YES animated:YES];
self.view.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.bounds.size.height, 0, 0, 0);

显示时这样:

[self.navigationController setNavigationBarHidden:NO animated:YES];
self.view.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

AFAIK contentInset 不应由系统更改,它会更改应用程序的布局...

Have you tried setting the content insets when you hide/show the navigation bar?

Like this when hiding:

[self.navigationController setNavigationBarHidden:YES animated:YES];
self.view.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.bounds.size.height, 0, 0, 0);

and this when showing:

[self.navigationController setNavigationBarHidden:NO animated:YES];
self.view.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

AFAIK contentInset shouldn't be changed by the system, it would change the layout of your app...

不奢求什么 2024-12-18 23:15:54

我的解决方案是在viewDidload中添加:

self.automaticallyAdjustsScrollViewInsets = NO;    

我没有使用 [UINavigationController setNavigationBarHidden:animated:],而是使用:

self.navigationController.navigationBar.hidden = YES;//or NO   

隐藏导航栏并避免 contentOffset 更改。

并记住在 viewDidDisappear 或某个适当的地方设置隐藏状态:

self.navigationController.navigationBar.hidden = NO

My solution is add:

self.automaticallyAdjustsScrollViewInsets = NO;    

in viewDidload.

and instead of using [UINavigationController setNavigationBarHidden:animated:], I use:

self.navigationController.navigationBar.hidden = YES;//or NO   

to hide the navigationbar and avoid contentOffset change.

And remember to set the hidden state back in viewDidDisappear or some place appropriate:

self.navigationController.navigationBar.hidden = NO
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文