iPad上运行的iPhone应用程序中横向视图顶部的20px差距

时间:2010-10-02 23:49:17

标签: iphone ipad uiwebview landscape

我正在iPad模拟器中运行“仅iPhone”应用程序...当设备的方向更改为横向模式时,我有一个视图控制器,可以启动并以编程方式加载WebView。这可以在iPhone中游戏(在横向视图之上没有间隙),但是当在iPad中进行模拟时,在视图的顶部有一个20px(我认为?)的间隙。

以下是横向视图控制器viewDidLoad中加载WebView的代码:

    [super viewDidLoad];

// Initialize webview and add as a subview to LandscapeController's view
CGRect webFrame = [[UIScreen mainScreen] bounds]; // Use bounds to take up entire screen
self.myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
self.myWebView.scalesPageToFit = YES;
self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.myWebView.delegate = self;
[self.view addSubview: self.myWebView]; 

// remove status bar from top of screen
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];

将状态栏设置为默认值,没有影响。

我似乎无法弄清楚为什么iPhone会好起来,但是出现在iPad上? 有任何想法吗?提前谢谢!

3 个答案:

答案 0 :(得分:3)

屏幕边界在屏幕坐标中。您将webview添加为self.view的子视图;它的框架在self.view坐标中。你想填充你的视图,而不是屏幕(你的视图会被UIViewController / UIKit的其余部分自动调整大小,最终应该调整Web视图的大小以自动调整大小):

self.myWebView = [[[UIWebView alloc] initWithFrame:self.view.bounds] autorelease];

更改-viewDidLoad中的状态栏是不安全的。视图加载可以在任何地方发生(当任何调用viewController.view时都会发生)。我也不确定你为什么要设定风格;你想设置隐藏:

  • 在-viewWillAppear:中,执行[application setStatusBarHidden:YES animated:animated];
  • 在-viewWillDisappear:中,执行[application setStatusBarHidden:NO animated:animated];

最后,您可能会看到不同的行为,因为iPad正在运行OS 3.2.x并且手机正在运行3.1.x或4.x.此外,iPhone兼容模式使用虚拟状态栏; “真实”状态栏始终位于屏幕边缘。

答案 1 :(得分:0)

顶部的20个像素几乎总是与状态栏的高度相关。

您发布的代码看起来不错......用于设置UIWebView。但是您将其作为子视图添加到视图控制器的view。那个大小怎么样?什么是frame

答案 2 :(得分:0)

我终于弄明白了。我在原帖中没有提到的关键缺失组件是管理格局的视图控制器实际上是作为模态视图实现的。 (有关如何执行此操作的代码,请参阅“View Controller用户指南”)在概念上,我有一个纵向视图控制器。 (这是主控制器)在纵向视图控制器的viewDidLoad中,我申请了一个由方向更改触发的通知程序,如下所示:

- (void)viewDidLoad {
[super viewDidLoad];

// SECTION to setup automatic alternate landscape view on rotation
// Uses a delegate to bring the landscape view controller up as a modal view controller

isShowingLandscapeView = NO;
// Create Landscape Controller programmatically
self.landscapeViewController = [[LandscapeViewController alloc] initWithNibName:@"LandscapeViewController" bundle:[NSBundle mainBundle]];

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(orientationChanged:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

// END SECTION landscape modal view controller

然后,当方向改变时,调用此方法:

- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
    // Load Landscape view
    landscapeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:self.landscapeViewController animated:YES];

    isShowingLandscapeView = YES;
}

同时我从Landscape视图控制器的viewWillAppear方法中删除了状态栏:

- (void)viewWillAppear:(BOOL)animated
{
// remove status bar from top of screen
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:animated];

self.myWebView.delegate = self; // setup the delegate as the web view is shown
}

这就是引入问题的地方。纵向视图控制器捕获屏幕尺寸,然后转换为横向视图作为模态视图。然后,在Landscape视图控制器中,viewWillAppear将删除状态栏。

因此,解决方案是移动

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:animated];
在转换到横向模态视图之前,在纵向视图控制器中对orientationChanged方法的

语句。

- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
    // remove status bar from top of screen
    // NOTE: this must be declared BEFORE presenting the Modal View!!!! If it's not, the landscape view will
    //       contain an ugly white bar in place of the missing status bar at the top of the view.
    [[UIApplication sharedApplication] setStatusBarHidden:YES];

    // Load Landscape view
    landscapeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:self.landscapeViewController animated:YES];

    isShowingLandscapeView = YES;
}

注意,正如上面提到的那样,如果你想要在回到肖像时出现状态栏,那么你需要

[[UIApplication sharedApplication] setStatusBarHidden:NO animated:animated];

在横向视图控制器中的viewWillDisappear方法中。