Search code examples
iosobjective-cuiwebviewuiinterfaceorientationdevice-orientation

Can't detect orientation of device at launching app to create UIWebView with correct size


If I started my app I want to know the orientation of my device. In my main view controller I have a UIWebView which I added programmatically to the main view controller.

I tested it with (int)[[UIApplication sharedApplication] statusBarOrientation] but I only get the number 1. No matter if my device is in portrait or in landscape.

I want to set the size of my UIWebView but if I not know the orientation how can I set the size of it?

2016-01-21 17:21:45.387 iPhoneApp[1026:151720] 0) Orientation: 1
2016-01-21 17:21:45.388 iPhoneApp[1026:151720] 0) Orientation: 0
2016-01-21 17:21:45.393 iPhoneApp[1026:151720] 1) Orientation: 1
2016-01-21 17:21:45.393 iPhoneApp[1026:151720] 1) Orientation: 0
2016-01-21 17:21:45.406 iPhoneApp[1026:151720] 2) Orientation: 1
2016-01-21 17:21:45.406 iPhoneApp[1026:151720] 2) Orientation: 1
2016-01-21 17:21:45.417 iPhoneApp[1026:151720] 3) Orientation: 1
2016-01-21 17:21:45.417 iPhoneApp[1026:151720] 3) Orientation: 1

0 is in didFinishLaunchingWithOptions

1 is in viewDidLoad

2 is in viewDidAppear

3 is in my method where I create my UIWebView

NSLog(@"3) Orientation: %d", (int)[[UIApplication sharedApplication] statusBarOrientation]);
NSLog(@"3) Orientation: %d", (int)[[UIDevice currentDevice] orientation]);

That's always the order of my log.

The method where I created my UIWebView looks like

-(void)createWebView {
    //NSLog(@"createWebView");
    //NSLog(@"orientation: %d", [[self orientNr] intValue]);
    [self destroyWebView];
    NSLog(@"3) Orientation: %d", (int)[[UIApplication sharedApplication] statusBarOrientation]);
    NSLog(@"3) Orientation: %d", (int)[[UIDevice currentDevice] orientation]);
    if( ([self firstLoad] && [[self orientNr] isEqualToNumber:[[NSNumber alloc] initWithInt:2]]) ||
        ([self firstLoad] && [[self orientNr] isEqualToNumber:[[NSNumber alloc] initWithInt:1]] && UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])))
        [self setWebView:[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)]];
    else
        [self setWebView:[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]];
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self view] addSubview:[self webView]];
    });
    [[self webView] setScalesPageToFit:YES];
    [[self webView] setDelegate:self];
    [[self webView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    [[self view] setAutoresizesSubviews:YES];
    //NSLog(@"iPhoneApp: %@", [self webView]);
}

The variable firstLoad checks if the app is started yet. orientNr is a number to rotate the app to the given orientation I want, not important for the solution (1 is device and 2 is landscape). webView is of type UIWebView.

So If I started the app and my device in portrait I want to create the webView with 320 width and 568 height. If it's at startup in landscape I want create the webView with 568 width and 320 height.

My problem is to detect the orientation of my device at startup. I also searched and found some threads like (Get launch orientation of iPad app or iOS: Device orientation on load) but it doesn't solved my problem

EDIT:

It only happens if I have my device on landscape or the UIWebView should start in landscape at launching the app

EDIT 2:

Problem solved. I only added

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [[self webView] setFrame:[[self view] bounds]];
}

and my UIWebView has always the same size like my UIView. In landscape orientation too. The answer here (https://stackoverflow.com/a/24398249/5629933) solved my problem. I'm new to Objective-C but does is make sense although I set the size of my UIWebView to the same like my UIView when I create it?


Solution

  • Most of the code in your createWebView method isn't needed. The big thing is to set the autoresizingMask before adding the web view.

    -(void)createWebView {
        //NSLog(@"createWebView");
        self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
        self.webView.scalesPageToFit = YES;
        self.webView.delegate = self;
        self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:self.webView];
    }