jclark754
jclark754

Reputation: 1014

UIWebView: Forcing to landscape (locking) and aspectfit to window

All, I have a UIWebView inside a ViewController. The controller is hooked up appropriately and segues properly from the previous tableview. However, I am struggling to accomplish two things: 1. Force the UiWebView (well, the entire scene) to landscape (I am displaying maps from a url) and not allow user rotation, and 2. Have the webview displayed within the constraints of the device. Right now, the UIWebView runs off the screen when displayed as a landscape.

I have tried to reset constraints, update missing constraints, direction lock in storyboard, view>mode>aspectfit in storyboard and forcing it to landscape in the viewcontroller method file (setting to aspectfit instead of aspectfill).

I have read previously questions on here which said it was not possible in previous iOS versions to lock just one scene to portrait/landscape. I have found that this might have changed with iOS7.

I am seeking guidance on how to lock a view and to shrink the webpage displayed in uiwebview to fit the dimensions of the iphone app. Thanks

Upvotes: 2

Views: 1175

Answers (2)

Justin
Justin

Reputation: 21

If its the entire application that you'd like to lock into landscape, you can go into your info.plist file and remove the portrait entries from the 'Supported interface orientation' list.

Upvotes: 0

ebandersen
ebandersen

Reputation: 2352

Four steps:

  1. subclass UINavigationController
  2. override the UINavigationController's preferredInterfaceOrientationForPresentation: method

    (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    
    UIDevice *myDevice = [UIDevice currentDevice];
    UIDeviceOrientation deviceOrientation = myDevice.orientation;
    
    if (deviceOrientation == UIDeviceOrientationLandscapeLeft)
    {
        return UIInterfaceOrientationLandscapeRight;
    }
    else if (deviceOrientation == UIDeviceOrientationLandscapeRight)
    {
        return UIInterfaceOrientationLandscapeLeft;
    }
    else {
        return UIInterfaceOrientationLandscapeLeft;
    }
    

    }

  3. set your UIViewController that has the UIWebView as the rootViewController of your subclassed UINavigationController

  4. Present your navigationController

If the above doesn't work, try applying the following to your tableViewController (i.e. the viewController that will present your UINavigationController subclass.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

You may also need to add these settings under Target > General. Device Orientation

That seems to be all that I did to get a forced landscape orientation working in my app.

Upvotes: 0

Related Questions