我有一个UIView
和一个UIController
视图。我的标准视图大小是320x460。在applicationDidFinishLaunching
中,我执行以下操作:
[window addSubview:[controller view]];
奇怪的是,UIView
在状态栏下面(就像缺少连接线一样)。然而,如果我将iPhone旋转到侧面,然后再转回来,它就可以正常显示了。
这是预期的行为吗(我猜可以通过设置偏移量来解决它),还是我的操作有问题?
我有一个UIView
和一个UIController
视图。我的标准视图大小是320x460。在applicationDidFinishLaunching
中,我执行以下操作:
[window addSubview:[controller view]];
奇怪的是,UIView
在状态栏下面(就像缺少连接线一样)。然而,如果我将iPhone旋转到侧面,然后再转回来,它就可以正常显示了。
这是预期的行为吗(我猜可以通过设置偏移量来解决它),还是我的操作有问题?
当使用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)];
}
}
if showing the status bar :
[controller view].frame = CGRectMake(0, **20**, 320, 460);
else
[controller view].frame = CGRectMake(0, 0, 320, **480**);
今天我添加了这个问题。结果发现我在ViewController的属性检查器中勾选了“想要全屏”。
关闭“想要全屏”解决了这个问题。
修复窗口对我没用,因为我有一个模态视图。这是一个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;
}
最终,我找到了这个解决方案。对于都有效:
- (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;
}
顺便提一下,出于某种原因,视图的高度是正确的:480(iPhone纵向模式下的屏幕高度)-20(状态栏)=460,但无法设置垂直偏移量。这是非常奇怪的行为,看起来像是一个漏洞。
这似乎是iOS 5中的一个错误。一个解决方法是在需要以模态方式呈现的任何视图控制器中使用wantsFullScreenLayout
,并手动布局视图始终位于状态栏下方。
对于 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);
}
我更喜欢使用UINavigationController来包装UIViewController,然后将NavigationBarHidden设置为UINavigationController。这是完美的解决方案,因为UINavigationController确实处理iOS 7中状态栏的高度。
以下是我的代码和屏幕截图。
UINavigationController *wrapNavController = [[UINavigationController alloc] initWithRootViewController:yourViewController] ;
[wrapNavController setNavigationBarHidden:YES] ;
[[controller view] setNeedsLayout];
,或者可能是[window layoutSubviews];
。我从来没有在使用它们来解决布局问题方面取得过很大的成功,但由于它在旋转后起作用,所以值得一试。