0

I am trying to add a subview on a landscape only app in a landscape only mode. This seems to work fine on newer iPads, However when testing this on an iPad 2 the subview only seems to appear in a portrait mode. What could I be doing wrong here?

Edit: This UIView shows fine on IOS 8 and above but Does not show up properly for IOS 7.1

Screen Shot of the xib properties

The Place where I add the subview:

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

Now in my MobileWebViewController I have this:

- (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;
}

where MobileWebView is:

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

from the xib file.

From what I could debug and see . The screen orientation seems to be still landscape but this particular view came in portrait possibly because of a different co ordinate system.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ducktales
  • 312
  • 4
  • 11

1 Answers1

0

I realized on that before IOS 8 The values of Screen size returned did not change according to the orientation of the App but from IOS8 onwards it does.

Basically wherever I was calling CGRectMake to Create the rectangle I was using the wrong values of Height and Width to Create my webview Frames.

I now used this:

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

where boundsForOrientation is :

 + (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;
 }

This gets the new width and height of the screen according to its orientation. I use these screen height and width values whenever I am manually drawing frames and views.

Ducktales
  • 312
  • 4
  • 11