仅在横向显示子视图时,iOS 7上的肖像在IOS 8上正常工作

时间:2015-02-21 04:49:47

标签: ios objective-c ipad uiwebview screen-orientation

我正在尝试在仅横向模式下在仅横向应用上添加子视图。这似乎适用于较新的iPad,但是当在iPad 2上进行测试时,子视图似乎只能以纵向模式显示。我在这里做错了什么?

编辑:此UIView在IOS 8及更高版本上显示正常,但未在IOS 7.1中正确显示

Screen Shot of the xib properties

我添加子视图的地方:

 MobileWebViewController *mobileViewController = [[MobileWebViewController alloc]
                initWithNibName:@"MobileWebViewFrame" 
                bundle:[NSBundle mainBundle]];
 [mobileViewController setUrlAddress:@"http://www.stackoverflow.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文件。

从我可以调试和看到。屏幕方向似乎仍然是风景,但这种特殊的观点可能是由于不同的坐标系统而出现的。

1 个答案:

答案 0 :(得分:0)

我在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;
 }

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