iPhone UIViewController进入状态栏

时间:2009-12-09 02:04:22

标签: iphone ios objective-c layout uiviewcontroller

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

[window addSubview:[controller view]];

奇怪的是,UIView位于状态栏下(就像缺少出口一样)。但是,如果我将iPhone向侧面旋转然后再向后旋转,则显示确定。

这是一种预期的行为(我打赌我可以通过设置偏移来修复它)或者我做错了吗?

11 个答案:

答案 0 :(得分:18)

我在通过 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)];
    }
}

答案 1 :(得分:8)

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

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

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

答案 2 :(得分:3)

最后,我得到了这个解决方案。适用于iPhone和iPad的效果很好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。由于某种原因,视图具有正确的高度 480(iPhone纵向模式下的屏幕高度) - 20(状态栏)= 460, 但未能设置垂直偏移。这是非常奇怪的行为,看起来像bug。

答案 3 :(得分:3)

修复窗口对我来说不起作用,因为我有一个模态视图。 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;
}

答案 4 :(得分:1)

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

答案 5 :(得分:1)

这似乎是iOS 5中的一个错误。一个修复方法是在任何视图控制器需要以状态栏下方模式和手动布局视图时使用wantsFullScreenLayout

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

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

答案 6 :(得分:0)

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

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

答案 7 :(得分:0)

即便是我也遇到了同样的问题。当我们使用一些编码进行设备定位时,我们在app delegate或视图控制器中编写了一些编码。我们需要更改条件以使用方向返回YES或NO。这解决了我们的问题。

答案 8 :(得分:0)

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

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

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

[wrapNavController setNavigationBarHidden:YES] ;

答案 9 :(得分:0)

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

答案 10 :(得分:0)

对于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);
}