Ash Var
Ash Var

Reputation: 241

How lock screen in iOS for it to have only portrait orientation?

I have created an app with many views and I want to have some of them only in portrait orientation. I have coded this in .m file:

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

Do I need to do something else? May be in .h file?

Upvotes: 4

Views: 11069

Answers (5)

da Rocha Pires
da Rocha Pires

Reputation: 2492

Cleanest solution for me:

Go to Info.plist file and for the "Supported interface orientations", remove every value except "Portrait (bottom home button)".

Upvotes: 5

objc_developer
objc_developer

Reputation: 71

Try this..

 -(BOOL)shouldAutorotate
 {
  return NO;
 }

-(NSUInteger)supportedInterfaceOrientations
 {
 return UIInterfaceOrientationMaskPortrait;
 }

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

Upvotes: 0

Timotei
Timotei

Reputation: 1909

You just need to return a BOOL in that method. If you want just portrait mode, that means:

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

If it's fine to be also Portrait Upside down (when in portrait rotate the device 180 degrees), then the method will look like:

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

The last condition can be replaced with a call to UIDeviceOrientationIsPortrait(interfaceOrientation), which does the same comparison (cf: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html)

LE: If this doesn't work, you can try using the follow 'hack': try to pop and push the view again (if you're using NavigationController). You can use the popViewControllerAnimated and pushViewController:animated: methods to force the controller re-query the required orientation :) Source: http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html)

Upvotes: 4

blacklion9279
blacklion9279

Reputation: 45

add following methods to your viewcontroller. This will ensure only portarait mode is supported for example.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

Upvotes: 1

DrummerB
DrummerB

Reputation: 40201

You have to return YES for the orientations you support (portrait), not simply NO for everything. Also make sure that in your project's target's settings you only check portrait mode as supported.

Upvotes: 2

Related Questions