setStatusBarHidden:NO 在 XIB 加载覆盖 UINavigationBar 后
当 setStatusBarHidden:NO
在视图加载之前设置时,UINavigationBar
和其他元素会在 StatusBar 的正下方对齐正如他们应该的那样。但是,当视图加载之后设置setStatusBarHidden:NO
时,UINavigationBar
会被部分覆盖。
加载上述视图后必须显示 StatusBar,但是如何才能在不遇到上述问题的情况下完成此操作呢?
When setStatusBarHidden:NO
is set before the view loads, the UINavigationBar
and other elements appear aligned immediately below the StatusBar as they should. However, when setStatusBarHidden:NO
is set after the view loads, the UINavigationBar
is partially covered.
The StatusBar must be revealed after loading the said view, but how can this be done without encountering the aforementioned problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在我的代码中发现了一个黑客攻击,但不记得或找不到它来自哪里。诀窍是通过隐藏和重新显示导航栏来刷新它:
在我的代码中,该函数如下所示:
但是,警告,这是一个黑客行为,目前我正在努力解决一些似乎源自此代码的错误(导航项与导航内容不匹配)。但由于它在某些地方确实对我有用,所以我想我应该提到它。
编辑:
我想我在这里找到了最初的帖子:
如何获取UINavigationController 中的导航栏在状态栏隐藏时更新其位置?
GL,
奥德
I found a hack in a code of mine, though can't remember or find where it came from. The trick is to refresh the navigation bar by hiding and reshowing it:
In my code the function looks like this:
However, BE WARNED, this is a hack, and currently I'm struggling with some bugs that appear to originate from this code (navigation item doesn't match navigation content). But since it did work for me in some places, I'd thought I'd mention it.
Edit:
I think I found the initial post here:
How do I get the navigation bar in a UINavigationController to update its position when the status bar is hidden?
GL,
Oded
(我意识到这是一个老问题,但我只是花了半个小时试图自己找到答案,但没有成功,所以我想我会把它发布在这里,供其他遇到困难的人......特别是如果你想展示状态栏和您的视图最终重叠)
我发现如果您想隐藏状态栏,这可以工作...
但当您想显示状态栏时则不行...
在这种情况下,我使用这个可行的解决方案,但让我担心,因为它将状态栏高度硬编码为 20...
它还让我担心我必须根据方向不同地调整视图。但如果我不这么做,20分的差距总是在错误的边缘。
就我而言,我想关闭某些视图的状态栏,然后在返回时重新打开。如果我在酒吧关闭时旋转设备,我会遇到特别的问题。所以 switch 语句虽然丑陋(有人可能会发布更干净的解决方案),但可以工作。
(I realise this was an old question, but I just spent half an hour trying to find the answer myself without success, so I thought I would post it here for anyone else who get stuck... especially if you are trying to SHOW the status bar and your view is ending up overlapping it)
I found this works if you want to HIDE the status bar...
but not when you want to SHOW the status bar...
in that case I use this solution which works, but worries me because it hard codes the status bar height to 20...
it also worries me that I have to adjust the view differently depending on orientation. but if I didn't do that it always had the 20 point gap on the wrong edge.
In my case I want to turn the status bar off for some views, and then back on when I return. I had particular problems if I rotated the device while the bar was off. so the switch statement, although ugly (someone might post a cleaner solution), works.
我的猜测是导航栏在状态栏显示之前加载,因此导航栏的位置是 (0,0),然后与 (0,0) 处的状态栏重叠。调用
setStatusBarHidden:NO
后,您可以在viewDidLoad
中移动导航栏的框架(或设置动画块)。尝试执行
navigationBar.frame = CGRectMake(0,20,320,44);
状态栏为 320x20,因此只需将导航栏向下移动 20 即可适应。
My guess is the nav bar is being loaded before the status bar is shown, so the position of the nav bar is (0,0) which then overlaps with the status bar at (0,0). You can just move the frame of the navigation bar (or set up an animation block) in
viewDidLoad
, after you callsetStatusBarHidden:NO
.Try doing
navigationBar.frame = CGRectMake(0,20,320,44);
The status bar is 320x20, so just moving your navigation bar down by 20 should accomodate for it.
如果您遇到此问题是因为在加载
Default.png
时未显示状态栏,然后希望在查看第一个视图控制器时立即显示状态栏,只需确保将[[UIApplication shareApplication] setStatusBarHidden:NO];
在AppDelegate.m
中的[self.window makeKeyAndVisible];
之前。它发生得如此之快,您将永远不会在启动屏幕上看到状态栏。If you are having this problem because you are not displaying the status bar while your
Default.png
is loading, and then want to display the status bar immediately upon viewing your first View Controller, just make sure you put[[UIApplication sharedApplication] setStatusBarHidden:NO];
before[self.window makeKeyAndVisible];
in yourAppDelegate.m
. It happens so quick, you won't ever see the status bar on the splash screen.这是我现在在 iOS 5 中告诉状态栏动画后在根控制器中所做的事情。丑陋,但它似乎有效。
Here's what I'm doing in my root controller now in iOS 5 after I tell the status bar to animate in. Ugly, but it seems to work.
在 iOS 7 中,您可以使用:
例如:
apple docs:
仅适用于 iOS 7。
in iOS 7 you can use:
for example:
apple docs:
use it only for iOS 7.