当navigationBarHidden设置为YES时如何防止UIView稍微向下滑动
基本上,我将全屏 UIViewController 推入 UINavigationController 中,并希望切换 navigationBarHidden 是否可见。
然而我发现当我将navigationBarHidden设置为YES后,整个UIView会向下滑动44像素,为navigationBar腾出空间。如何禁用此行为?由于我已经将 barStyle 设置为 UIBarStyleBlackTranslucent 并且滑动确实没有必要。
//init viewController
[self.navigationController pushViewController:viewController animated:YES];
编辑:
我还在处理将滚动视图推送到导航控制器。我发现在将导航栏设置为可见之后。我的滚动视图将被向下推一点,当我在该状态下手动滚动视图时,滚动视图本身将再次向上滑动。我的问题是如何防止scrollView向下滑动。
框架尺寸与视图向下或向上滑动之前相同。
Basically I pushed a full screen UIViewController into a UINavigationController and would like to toggle whether the navigationBarHidden is seem.
Howerever I find that after I set navigationBarHidden to YES, the whole UIView will slide down a 44 pixels to make the room for navigationBar. How to I disable this behavior? Since I already set barStyle to UIBarStyleBlackTranslucent and the sliding off is really not necessary.
//init viewController
[self.navigationController pushViewController:viewController animated:YES];
Edit:
I am also dealing with pushing a scrollview to the navigation controller.. I find that after setting the navigation bar to be visible. My scroll view will be pushed down a little bit, when I scroll the view manually in that state, the scrollView itself will slide up again. My question is how to prevent the scrollView from sliding down.
Frame size is the same before the view slide down or up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试重置视图框架。将栏设置为隐藏后,执行以下操作:
您始终可以将其打破并查看值是什么,甚至可以相应地构建自定义框架以使其适合。我以前也必须这么做过...
Try resetting the view frame. Do something like this after you set the bar to hidden:
You can always break it there and see what the values are and could even build a custom frame accordingly to make it fit. I have had to do that before...
我通过将scrollView添加到UIView并将UIView设置为UIViewController的视图解决了这个问题。
I solved this problem by adding the scrollView to a UIView and set the UIView as UIViewController's view.
我认为这是 UINavigationController 的一个错误/功能,我不知道有什么方法可以禁用它。太糟糕了。我总是不得不手动重新调整我的视图框架。在处理旋转、滚动视图时,这比应有的痛苦要多得多......
I think this is a bug/feature of UINavigationController and I'm not aware of any way to disable it. It sucks. I've always had to readjust my view frames manually. This is way more of a pain than it should be when dealing with rotation, scroll views....
可能与 contentInset 有关。设置navigationBarHidden属性时,如果当前视图控制器的根视图是滚动视图,则其contentInsets.top设置为顶部栏的高度。
为了防止这种情况,在更改 navigationBarHidden 的行之后,立即将 self.scrollView.contentInset 重置为您拥有的任何原始值。
Might have something to do with contentInset. When setting navigationBarHidden property, if root view of the current view controller is a scroll view, its contentInsets.top is set to the height of the top bars.
To prevent that, immediately after the line that changes navigationBarHidden, reset self.scrollView.contentInset to whatever original value you have.
在 viewDidLoad 方法中添加以下内容:
它告诉视图控制器已经将子视图放置在导航栏下方。
Add the following in your viewDidLoad method:
It tells the view controller to already place subviews under the navigation bar.
这里的答案都不适合我。相反,您可以在
viewDidLayoutSubviews()
期间重置UIScrollView
上的插图,如下所示(在 Swift 中):None of the answers here worked for me. Instead, you can reset the inset on the
UIScrollView
duringviewDidLayoutSubviews()
as seen below (in Swift):我试过了,唯一有效的方法是移动navigationBar的原点y,并将navigationBar的subViews的alpha更改为0.f;如果想让它显示出来,只需反向操作即可
I has tried, which works only is that move navigationBar with its origin y, and change navigationBar's subViews's alpha to 0.f; if want it show back, just do it reverse
self.navigationController.navigationBar.alpha = 0.0;
这是一个简单的解决方案
self.navigationController.navigationBar.alpha = 0.0;
this can be a easy solution