简体   繁体   中英

change one view orientation to landscape

I'm working on a project in which user will watch live channels. But I'm facing a small problem here and I've tried very hard but failed to find any solution. My app will support portrait orientation for all views but last one. Last view will support only landscape orientation. Is it possible? I've searched and tried following code

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

//

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

//

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

Follow this link . Hope you get your answer here.

The link shows how to keep all your views in portrait mode except one view that will be in landscape.

You need to do the following:

1st :

Implement this in all controllers that are fix for portrait:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

And implement this for the landscape controllers:

- (BOOL)shouldAutorotate
{
    return YES;
}


- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

2nd :

 // Fetch the status bar from the app and set its orientation as required. 
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

// Create an empty view controller, present it modally and remove it directly afterwards
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
// Now the device is rotated to the desired orientation. Go from there.

Hope that works.

From iOS6 apple has changed orientation update mechanism. From iOS6 onwards, iOS will report orientation event only to RootController, so all the orientation related decision can only be taken in RootController. Lets say you are using UINavigationController or UITabBarController as window's root controller. In this case you can create sub class of UINavigationController or UITabBarController, override orientation related methods and pass orientation related events to childControllers. Set this custom UINavigationController or UITabBarController object as rootController of your window.

You can override below methods in your custom class.

-(BOOL)shouldAutorotate
{
    BOOL shouldRotate = YES;
    if(self.viewControllers.count > 0)
        shouldRotate = [[self.viewControllers lastObject] shouldAutorotate];
    return shouldRotate;
}

-(NSUInteger)supportedInterfaceOrientations
{
    NSUInteger supportedInterfaces = UIInterfaceOrientationMaskAll;
    if(self.viewControllers.count > 0)
        supportedInterfaces = [[self.viewControllers lastObject] supportedInterfaceOrientations];
    return supportedInterfaces;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    UIInterfaceOrientation preferredOrientation = UIInterfaceOrientationPortrait;
    if(self.viewControllers.count > 0)
        preferredOrientation = [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
    return preferredOrientation;
}

Use Following approach : In your app delegate .h

@interface PlayWithWSWithLibAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

       BOOL flagOrientationAll;
}

@property (assign) BOOL flagOrientationAll;

Add following method in your app delegate .m file

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    //NSLog(@"PlayWithWSWithLibAppDelegate -- supportedInterfaceOrientationsForWindow");
    if(flagOrientationAll == YES){
        return UIInterfaceOrientationMaskLandscapeRight;
    } else {
        return UIInterfaceOrientationMaskPortrait ;  // your Default orientation for all other view
    }
 }

Implement following way in your view which you want to rotate in both portrait and landscape both for iPhone device

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.delegate = self;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM