ios iPhone UIViewController 进入状态栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1871189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:41:55  来源:igfitidea点击:

iPhone UIViewController goes under status bar

iphoneiosobjective-clayoutuiviewcontroller

提问by Mantas

I have a UIViewand a UIControllerview. My is standard a 320x460 view. In applicationDidFinishLaunchingI do:

我有UIView一个UIController观点。我的标准是 320x460 视图。在applicationDidFinishLaunching我做:

[window addSubview:[controller view]];

The weird thing is that the UIViewgoes under the status bar (like there's missing outlet). However, if I rotate iPhone to the side and then back, it shows up ok.

奇怪的是,UIView状态栏下面的(就像缺少插座一样)。但是,如果我将 iPhone 旋转到一边然后再旋转,它显示正常。

Is this an expected behavior (I bet I can fix it by setting offset) or am I doing smth wrong?

这是预期的行为(我打赌我可以通过设置偏移量来修复它)还是我做错了什么?

采纳答案by n3wscott

I think your problem is that when you add a view to a window, you need to be aware of the state of the status bar and compensate for it:

我认为您的问题是,当您向窗口添加视图时,您需要了解状态栏的状态并对其进行补偿:

if showing the status bar :
   [controller view].frame = CGRectMake(0, **20**, 320, 460);
else 
   [controller view].frame = CGRectMake(0, 0, 320, **480**);

this is why IB shows you a dummy status bar.

这就是 IB 向您显示一个虚拟状态栏的原因。

回答by Nick Baicoianu

I ran into this issue when displaying a UIViewController via presentModalViewController.

我在通过presentModalViewController显示 UIViewController 时遇到了这个问题。

You can get around it by manually resizing the controller's view afterthe view has appeared:

您可以通过在视图出现手动调整控制器视图的大小来绕过它:

- (void) viewDidAppear: (BOOL) animated {
    //manually adjust the frame of the main view to prevent it from appearing under the status bar.
    UIApplication *app = [UIApplication sharedApplication];
    if(!app.statusBarHidden) {
        [self.view setFrame:CGRectMake(0.0,app.statusBarFrame.size.height, self.view.bounds.size.width, self.view.bounds.size.height - app.statusBarFrame.size.height)];
    }
}

回答by user1626682

I add this issue today. It turned out that I had "Wants Full Screen" checked in the ViewController's Attribute inspector.

我今天补充这个问题。原来我在 ViewController 的属性检查器中选中了“想要全屏”。

Turning off "Wants Full Screen" resolved the problem.

关闭“想要全屏”解决了这个问题。

回答by Mike Keskinov

Finally, I got to this solution. Works well for both iPhone & iPad:

最后,我得到了这个解决方案。适用于 iPhone 和 iPad

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Allocate view controller and load nib file
    if (isIPhone) {
        self.mainViewController = [[[tfdMainViewController_iPhone alloc] initWithNibName:@"tfdMainViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.mainViewController = [[[tfdMainViewController_iPad alloc] initWithNibName:@"tfdMainViewController_iPad" bundle:nil] autorelease];
    }

    // Offset correction (iPhone bug?)
    CGRect r = self.mainViewController.view.frame; 
    r = CGRectOffset(r, 0, [UIApplication sharedApplication].statusBarFrame.size.height);
    [self.mainViewController.view setFrame:r];  

    [window addSubview: self.mainViewController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

P.S. For some reason view has correct height: 480 (screen height in iPhone Portrait mode) - 20 (status bar) = 460, but failed to set vertical offset. It is pretty strange behavior, looks like bug.

PS 由于某种原因,视图具有正确的高度:480(iPhone 纵向模式下的屏幕高度)- 20(状态栏)= 460,但未能设置垂直偏移。这是非常奇怪的行为,看起来像错误。

回答by Jon Ramvi

Fixes to the window didn't work for me as I had a modal view. A UIModalPresentationCurrentContext modal view. OK, here's what worked for me. I've been searching the web up and down before getting this to work. I'm unable to move the view.frame from the parent. However in the viewWillAppear I'm able to move it down:

修复窗口对我不起作用,因为我有一个模态视图。一个 UIModalPresentationCurrentContext 模态视图。好的,这对我有用。在让它工作之前,我一直在网上上下搜索。我无法从父级移动 view.frame。但是在 viewWillAppear 中,我可以将其向下移动:

- (void)viewWillAppear:(BOOL)animated
{
    // Move down to compensate for statusbar
    CGRect frame = parentView.navCon.view.frame;
    frame.origin.y = [UIApplication sharedApplication].statusBarFrame.size.height;
    parentView.navCon.view.frame = frame;
}

回答by markshiz

This appears to be a bug in iOS 5. One fix would be to use wantsFullScreenLayoutin whatever view controller needs to present modally and manually layout the view always below the status bar.

这似乎是 iOS 5 中的一个错误。一种解决方法是wantsFullScreenLayout在任何视图控制器需要以模态方式呈现并手动布局视图时使用始终位于状态栏下方。

http://www.cocoanetics.com/2012/06/radar-view-frame-inconsistency-using-presentviewcontroller-wantsfullscreenlayout-yn/

http://www.cocoanetics.com/2012/06/radar-view-frame-inconsistency-using-presentviewcontroller-wantsfullscreenlayout-yn/

http://openradar.appspot.com/radar?id=1758406

http://openradar.appspot.com/radar?id=1758406

回答by ucangetit

If you are using Interface Builder's "Simulated User Interface Elements", then you also need to make sure that you have set the flag for "Resize View From NIB" in your MainWindow nib.

如果您使用的是 Interface Builder 的“模拟用户界面元素”,那么您还需要确保在 MainWindow 笔尖中设置了“从 NIB 调整视图大小”的标志。

回答by Ian Henry

Are you manually setting the application bar hidden property AFTER adding the subview? I don't imagine this is the case, but if it's set to none when you first load the view it will layout as if there isn't one, and if you then set the status bar to not hidden it will pop up on top of your view.

您是否在添加子视图后手动设置应用程序栏隐藏属性?我不认为是这种情况,但是如果在您第一次加载视图时将其设置为无,它将布局为好像没有视图一样,然后如果您将状态栏设置为不隐藏,它将在顶部弹出你的看法。

A possible solution is to use [[controller view] setNeedsLayout];after adding the subview, or possibly [window layoutSubviews];. I've never had a lot of success using those to fix layout problems, but since it works after a rotation it's worth a shot.

一个可能的解决方案是[[controller view] setNeedsLayout];在添加子视图后使用,或者可能[window layoutSubviews];. 我从来没有在使用它们来解决布局问题方面取得很大成功,但由于它在旋转后工作,因此值得一试。

回答by SURESH SANKE

Even me too got the same issue. When we are using some coding for device orientation we have wrote some coding in app delegate or in our view controller. There we need to change the condition to use the orientation return YES or NO. That solved our issue.

甚至我也遇到了同样的问题。当我们为设备方向使用一些编码时,我们已经在应用程序委托或我们的视图控制器中编写了一些编码。我们需要更改条件以使用方向返回 YES 或 NO。那解决了我们的问题。

回答by Grant Fong

I prefer to use UINavigationController to wrap the UIViewController you, after that, set the NavigationBarHidden to the UINavigationController. It's perfect solution cause UINavigationController do handle the height of status bar in iOS 7.

我更喜欢使用 UINavigationController 来包装 UIViewController,然后将 NavigationBarHidden 设置为 UINavigationController。这是完美的解决方案,因为 UINavigationController 确实处理了 iOS 7 中状态栏的高度。

Here is my code and my screen capture.

这是我的代码和我的屏幕截图。

UINavigationController *wrapNavController = [[UINavigationController alloc] initWithRootViewController:yourViewController] ;

[wrapNavController setNavigationBarHidden:YES] ;