setStatusBarHidden:NO 在 XIB 加载覆盖 UINavigationBar 后

发布于 2024-10-20 13:16:16 字数 310 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(6

司马昭之心 2024-10-27 13:16:16

我在我的代码中发现了一个黑客攻击,但不记得或找不到它来自哪里。诀窍是通过隐藏和重新显示导航栏来刷新它:

[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.navigationController setNavigationBarHidden:NO animated:NO];

在我的代码中,该函数如下所示:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

但是,警告,这是一个黑客行为,目前我正在努力解决一些似乎源自此代码的错误(导航项与导航内容不匹配)。但由于它在某些地方确实对我有用,所以我想我应该提到它。

编辑:
我想我在这里找到了最初的帖子:
如何获取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:

[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.navigationController setNavigationBarHidden:NO animated:NO];

In my code the function looks like this:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

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

冷︶言冷语的世界 2024-10-27 13:16:16

(我意识到这是一个老问题,但我只是花了半个小时试图自己找到答案,但没有成功,所以我想我会把它发布在这里,供其他遇到困难的人......特别是如果你想展示状态栏和您的视图最终重叠)

我发现如果您想隐藏状态栏,这可以工作...

[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.view setFrame: [[UIScreen mainScreen] bounds]];

但当您想显示状态栏时则不行...
在这种情况下,我使用这个可行的解决方案,但让我担心,因为它将状态栏高度硬编码为 20...
它还让我担心我必须根据方向不同地调整视图。但如果我不这么做,20分的差距总是在错误的边缘。
就我而言,我想关闭某些视图的状态栏,然后在返回时重新打开。如果我在酒吧关闭时旋转设备,我会遇到特别的问题。所以 switch 语句虽然丑陋(有人可能会发布更干净的解决方案),但可以工作。

[[UIApplication sharedApplication] setStatusBarHidden:NO];

CGRect frame = [[UIScreen mainScreen] bounds];

switch (self.interfaceOrientation) 
{
    case UIInterfaceOrientationPortrait:
        frame.origin.y = 20;
        frame.size.height -= 20;
        break;

    case UIInterfaceOrientationPortraitUpsideDown:
        frame.origin.y = 0;
        frame.size.height -= 20;
        break;

    case UIInterfaceOrientationLandscapeLeft:
        frame.origin.x = 20;
        frame.size.width -= 20;
        break;

    case UIInterfaceOrientationLandscapeRight:
        frame.origin.x = 0;
        frame.size.width -= 20;
        break;

} 

[self.view setFrame:frame];

(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...

[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.view setFrame: [[UIScreen mainScreen] bounds]];

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.

[[UIApplication sharedApplication] setStatusBarHidden:NO];

CGRect frame = [[UIScreen mainScreen] bounds];

switch (self.interfaceOrientation) 
{
    case UIInterfaceOrientationPortrait:
        frame.origin.y = 20;
        frame.size.height -= 20;
        break;

    case UIInterfaceOrientationPortraitUpsideDown:
        frame.origin.y = 0;
        frame.size.height -= 20;
        break;

    case UIInterfaceOrientationLandscapeLeft:
        frame.origin.x = 20;
        frame.size.width -= 20;
        break;

    case UIInterfaceOrientationLandscapeRight:
        frame.origin.x = 0;
        frame.size.width -= 20;
        break;

} 

[self.view setFrame:frame];
╰沐子 2024-10-27 13:16:16

我的猜测是导航栏在状态栏显示之前加载,因此导航栏的位置是 (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 call setStatusBarHidden: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.

原谅我要高飞 2024-10-27 13:16:16

如果您遇到此问题是因为在加载 Default.png 时未显示状态栏,然后希望在查看第一个视图控制器时立即显示状态栏,只需确保将[[UIApplication shareApplication] setStatusBarHidden:NO];AppDelegate.m 中的 [self.window makeKeyAndVisible]; 之前。它发生得如此之快,您将永远不会在启动屏幕上看到状态栏。

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[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 your AppDelegate.m. It happens so quick, you won't ever see the status bar on the splash screen.

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.window makeKeyAndVisible];
鯉魚旗 2024-10-27 13:16:16

这是我现在在 iOS 5 中告诉状态栏动画后在根控制器中所做的事情。丑陋,但它似乎有效。

CGRect rect;

if ( self.interfaceOrientation == UIInterfaceOrientationPortrait )
    rect = CGRectMake(0, 20, 320, 460);
else if ( self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown )
    rect = CGRectMake(0, 0, 320, 460);
else if ( self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft )
    rect = CGRectMake(20, 0, 300, 480);
else
    rect = CGRectMake(0, 0, 300, 480);

[UIView animateWithDuration:0.35 animations:^{ self.view.frame = rect; }];

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.

CGRect rect;

if ( self.interfaceOrientation == UIInterfaceOrientationPortrait )
    rect = CGRectMake(0, 20, 320, 460);
else if ( self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown )
    rect = CGRectMake(0, 0, 320, 460);
else if ( self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft )
    rect = CGRectMake(20, 0, 300, 480);
else
    rect = CGRectMake(0, 0, 300, 480);

[UIView animateWithDuration:0.35 animations:^{ self.view.frame = rect; }];
想你只要分分秒秒 2024-10-27 13:16:16

在 iOS 7 中,您可以使用:

setNeedsStatusBarAppearanceUpdate

例如:

[self.mainViewController.navigationController setNeedsStatusBarAppearanceUpdate];

apple docs:

如果视图控制器的状态栏属性,例如
作为隐藏/未隐藏状态或样式进行更改。如果你调用这个方法
在动画块中,变化随着
动画块的其余部分。

仅适用于 iOS 7。

in iOS 7 you can use:

setNeedsStatusBarAppearanceUpdate

for example:

[self.mainViewController.navigationController setNeedsStatusBarAppearanceUpdate];

apple docs:

Call this method if the view controller's status bar attributes, such
as hidden/unhidden status or style, change. If you call this method
within an animation block, the changes are animated along with the
rest of the animation block.

use it only for iOS 7.

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