iPhone的UIViewController进入状态栏

我有一个UIView和一个UIController视图。 我是标准的320×460视图。 在applicationDidFinishLaunching我做:

 [window addSubview:[controller view]]; 

奇怪的是, UIView进入状态栏(就像缺less出口)。 但是,如果我将iPhone旋转到侧面,然后返回,则显示正常。

这是一个预期的行为(我敢打赌,我可以通过设置偏移量来解决)还是我做错了?

我想你的问题是,当你添加一个窗口的视图,你需要知道状态栏的状态,并补偿它:

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

这就是为什么IB向您显示一个虚拟状态栏。

当通过presentModalViewController显示一个UIViewController时遇到了这个问题。

在视图出现 ,您可以通过手动调整控制器的视图来解决该问题:

 - (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)]; } } 

我今天添加这个问题。 事实certificate,我有“要全屏”在ViewController的属性检查器中检查。

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

最后,我得到了这个解决scheme。 适用于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; } 

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

由于我有一个模态的观点,修复窗口对我来说不起作用。 UIModalPresentationCurrentContext模态视图。 好的,这是为我工作的。 在开始工作之前,我一直在searchnetworking。 我无法从父级移动view.frame。 不过在视图上,我可以把它往下移动:

 - (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; } 

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

这似乎是iOS 5中的一个错误。一种修复方法是在任何视图控制器中使用wantsFullScreenLayout来以模态方式呈现并手动布局总是位于状态栏下方的视图。

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

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

你是否手动设置应用程序栏隐藏属性后添加子视图? 我不认为这是事实,但是如果它在第一次加载视图时被设置为none,它将会像没有一个一样布局,如果你将状态栏设置为不隐藏,它会popup在顶部你的看法。

一个可能的解决scheme是使用[[controller view] setNeedsLayout]; 添加子视图后,或可能[window layoutSubviews]; 。 我从来没有很成功地使用这些来解决布局问题,但是因为它在旋转之后起作用,所以值得一试。

即使我也有同样的问题。 当我们在设备方向上使用一些编码时,我们在应用程序代理或视图控制器中编写了一些编码。 在那里我们需要改变条件来使用方向返回YES或NO。 这解决了我们的问题。

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

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

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

Xcode 6,iOS7 / 8解决scheme是取消选中“属性”检查器的“视图控制器”部分的“扩展边”部分中的“下方顶部条”复选标记。

对于Xamarin.iOS它将是:

 if (UIApplication.SharedApplication.StatusBarHidden == false) { var statusBarHeight = UIApplication.SharedApplication.StatusBarFrame.Height; View.Frame = new CoreGraphics.CGRect(0, statusBarHeight, View.Frame.Width, View.Frame.Height - statusBarHeight); }