MoLowKey
MoLowKey

Reputation: 1142

prevent change orientation in a viewcontroller

I have messaging app like whatsapp & ... The hierarchy like this :

Tab Bar Controller
   Navigation Controller
       View Controller
             Target Controller

so we are going to Tabbar like this from appDelegate

UITabBarController  *ivc = [storyboard instantiateViewControllerWithIdentifier:@"mainTab"];
self.window.rootViewController = ivc;
[self.window makeKeyAndVisible];

my problem I want to lock the orientation in some condition in a Target Controller but -(BOOL)shouldAutorotate supportedInterfaceOrientations doesn't call , i have read some post about this but i can't find the correct solution. the posts like this : Lock Screen Rotation in iOS 8

Handling autorotation for one view controller in iOS7

UPDATE

after many hour for searching , i found the solution but it's not enough this link hint to me , if we use UITabController every thing will change so , we should call

-(NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController
{
return navigationController.topViewController.supportedInterfaceOrientations;
  }

-(NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController
     {
return ((parentOfTarget*)[tabBarController.viewControllers objectAtIndex:0]).supportedInterfaceOrientations;
 }

this cause i give notification of change orientation & prevent it, but i will see orientation of status bar .

i think i need to call

navigationController.topViewController.shouldAutorotate;

but it's cause crash ;(

TabBarController: Orienting views in different orientations

Upvotes: 0

Views: 512

Answers (2)

Jagandeep Singh
Jagandeep Singh

Reputation: 364

Use this method to stop landscape orientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

    // Use this to allow upside down as well
    //return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

and if you want to use shouldAutorotate delegate method you can use in appdelegate.m .

- (BOOL)shouldAutorotate {
    return NO;
}

If you are using latest ios then use these methods :-

- (NSUInteger) supportedInterfaceOrientations {
    // Return a bitmask of supported orientations. If you need more,
    // use bitwise or (see the commented return).
    return UIInterfaceOrientationMaskPortrait;
    // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    // Return the orientation you'd prefer - this is what it launches to. The
    // user can still rotate. You don't have to implement this method, in which
    // case it launches in the current orientation
    return UIInterfaceOrientationPortrait;
}

Upvotes: 0

Abhinav
Abhinav

Reputation: 38142

You should use -supportedInterfaceOrientations' instead of-shouldAutorotate`.

Later should only be used when orientation is decided at run time. In your case, your view controller will always only support portrait mode.

Next, your navigation controller's delegate must implement following method to return the result of calling -supportedInterfaceOrientations on the view controller at the top of the navigation stack.

-(NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController {
    return navigationController.topViewController.supportedInterfaceOrientations;
}

Upvotes: 1

Related Questions