user3231429
user3231429

Reputation: 205

Return to portrait after landscape view

I have an application that need to be on portrait orientation for all view, and one view (UIWebview) that need to support portrait and lansdscape orietation.

To set the default portrait orientation , I put this in my app delegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
//NSLog(@"PlayWithWSWithLibAppDelegate -- supportedInterfaceOrientationsForWindow");
if(flagOrientationAll == YES){
    return UIInterfaceOrientationMaskAll;
} else {
    return UIInterfaceOrientationMaskPortrait;
}}

And I have manage to arrange my webview to portrait/landscape regarding the user orientation using this method (got this from stackoverflow also)

-(void)viewWillAppear:(BOOL)animated
{

AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
//NSLog(@"viewWillDisappear -- Start");
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = NO;
}

And to present portrait and landscape i using two UIView:

-(void)viewWillLayoutSubviews {
    BOOL layoutIsPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
    if (layoutIsPortrait) {
        self.view = self.pView;
    [self.pView addSubview:pwebView];
}else{
    self.view = self.lView;
    [self.lView addSubview:lwebView];
}}

Then to dismiss the webview i using this code:

- (IBAction)doneButtonTapped:(id)sender
{
   [self.navigationController popViewControllerAnimated:YES];
}

However when it dismiss in landscape state, the previous screen (the screen that call the webview) present in landscape instead of the default(portrait). How i can make after the webview dismiss it will still present in portrait?

thanks

Upvotes: 1

Views: 299

Answers (2)

Nico teWinkel
Nico teWinkel

Reputation: 880

Swift 3.0:
(Added to the ViewController that calls the landscape-able view)

override var shouldAutorotate: Bool {
    return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

Upvotes: 0

Matteo Gobbi
Matteo Gobbi

Reputation: 17687

You should implement the supported orientation for each viewController:

Like i wrote here:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    //Choose your available orientation, you can also support more tipe using the symbol |
    //e.g. return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)
    return (UIInterfaceOrientationMaskPortrait);
}

Upvotes: 1

Related Questions