仅横向子视图在 iOS 7 上显示为纵向,在 IOS 8 上工作正常

A landscape only subview appears as portrait on iOS 7 works fine on IOS 8

我正在尝试以仅横向模式在仅横向应用程序上添加子视图。这似乎在较新的 iPads 上工作正常,但是在 iPad2 上测试时,子视图似乎只出现在纵向模式下。我在这里做错了什么?

编辑:此 UIView 在 IOS 8 及更高版本上显示良好,但在 IOS 7.1

上显示不正确

添加子视图的地方:

 MobileWebViewController *mobileViewController = [[MobileWebViewController alloc]
                initWithNibName:@"MobileWebViewFrame" 
                bundle:[NSBundle mainBundle]];
 [mobileViewController setUrlAddress:@"http://www.whosebug.com"];  //user defined function
 [[UIApplication sharedApplication].keyWindow addSubview: mobileViewController.view];

现在在我的 MobileWebViewController 中我有这个:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
   return [[UIApplication sharedApplication] statusBarOrientation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

//For Older versions
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation 
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didRotateFromInterfaceOrientation:
(UIInterfaceOrientation)fromInterfaceOrientation
{
    [UIViewController attemptRotationToDeviceOrientation];
}


- (void) setUrlAddress:(NSString *)url
{
    self.urlAddress = url;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | 
    UIViewAutoresizingFlexibleWidth;
    [self setNeedsStatusBarAppearanceUpdate];
    [[NSUserDefaults standardUserDefaults] synchronize];

    self.MobileWebView.delegate = self;
    self.MobileWebView.scalesPageToFit = NO;
    self.MobileWebView.multipleTouchEnabled = NO;
    self.MobileWebView.frame = CGRectMake(0,
    self.MobileWebView.frame.origin.y,self.view.frame.size.width,
    self.view.frame.size.height - self.MobileWebView.frame.origin.y);
    self.MobileWebView.transform = CGAffineTransformIdentity;
}

其中 MobileWebView 是:

@property (strong, nonatomic) IBOutlet UIWebView *MobileWebView;

来自 xib 文件。

从我可以调试和看到的内容。屏幕方向似乎仍然是横向的,但这个特定的视图是纵向的,可能是因为坐标系不同。

我在IOS 8之前意识到了这一点返回的屏幕尺寸的值没有根据应用程序的方向而改变,但是从IOS8开始它会改变。

基本上无论我调用 CGRectMake 来创建矩形,我都使用了错误的高度和宽度值来创建我的 webview 框架。

我现在用这个:

    CGRect screenRect = [MobileAppHelper boundsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];

其中 boundsForOrientation 是:

 + (CGRect) boundsForOrientation:(UIInterfaceOrientation)orientation
 {
     CGFloat width   = [[UIScreen mainScreen] bounds].size.width;
     CGFloat height  = [[UIScreen mainScreen] bounds].size.height;

     CGRect bounds = CGRectZero;
     if (UIInterfaceOrientationIsLandscape(orientation))
     {
         bounds.size = CGSizeMake(MAX(width, height), MIN(width, height));
     }
     else
     {
         bounds.size = CGSizeMake(MIN(width, height), MAX(width, height));
     }
     return bounds;
 }

这会根据屏幕的方向获取屏幕的新宽度和高度。每当我手动绘制框架和视图时,我都会使用这些屏幕高度和宽度值。