-1

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?

Community
  • 1
  • 1
Premox
  • 323
  • 10
  • 25
  • 1
    Some of what you are doing is way too early, which is why you get useless results. But you shouldn't need to know the orientation of the device at all. Position your web view using auto layout. That way, no matter what happens, it will be in the right position. – matt Jan 21 '16 at 16:48
  • Ah okay I understand but the size is not set correctly. If you can see in my method where I create my `web view` the size of it is always 320 width and 568 height. Imagine if the orientation is set to landscape for this view my `web view` is in correct orientation but the size doesn't fit the whole screen. The same problem if my device is in landscape mode only the size of my website doesn't fit the whole screen because the width of my `UIView` is always 320 width and 568 height @matt. How I can solve it? – Premox Jan 21 '16 at 16:56
  • That screenshot makes no sense. What part of that is `self.view`? What part of that is supposed to be the web view? And what part is the page loaded into the web view? – rmaddy Jan 21 '16 at 17:17
  • @rmaddy I added another screenshot. My `web view` is on the left side with the content (the sentence). It should filled the whole screenshot and not only the half – Premox Jan 21 '16 at 17:23
  • That screen shot is even worse. It's just white. How can you tell whether the web view is really filling `self.view` or not? Set `self.view.backgroundColor = [UIColor greenColor];`. Run your app. Do you see any green? If not, then the web view is filling `self.view`. – rmaddy Jan 21 '16 at 17:44
  • It was my fault, sry. I filled the `UIView` green (`self.view.backgroundColor = [UIColor greenColor]`). Now we see that the size of my `UIWebView` doesn't fill my `self.view` – Premox Jan 22 '16 at 07:31

2 Answers2

1

Your entire createWebView approach is ill-advised. You should not be concerned with orientation at all. Just add your web view to self.view and position it using auto layout. That way, when layout finishes, it will be in the right position / size regardless of orientation or anything else that may happen to self.view.

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

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];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • I thought so too but if the website is configured to landscape when I started my app it doesn't fit my whole screen. It only has a size of 320 width and 568 height. The website has the correct orientation but the size of my `web view` is not correct. It only has the half size of landscape @rmaddy – Premox Jan 21 '16 at 17:04
  • I'm confused. Is your issue that the size of the web view isn't filling `self.view` or is the problem that the loaded website isn't filling the web view properly? – rmaddy Jan 21 '16 at 17:07
  • My issue is that my `web view` isn't filling my `self.view` – Premox Jan 21 '16 at 17:08
  • I do all of my apps strictly in code just like posted in my answer. Code like that should ensure that the web view will fill `self.view` no matter how the size of `self.view` changes. How do you know the web view isn't filling `self.view`? – rmaddy Jan 21 '16 at 17:11
  • I edited my post. It looks like this image. Adopted it isn't that problem why is it not fullscreen? – Premox Jan 21 '16 at 17:15
  • Thx helped me a lot with your questions and advices. – Premox Jan 22 '16 at 10:26