How lock screen in iOS for it to have only portrai

2019-06-24 15:20发布

问题:

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?

回答1:

Cleanest solution for me:

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



回答2:

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)



回答3:

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.



回答4:

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;
}


回答5:

Try this..

 -(BOOL)shouldAutorotate
 {
  return NO;
 }

-(NSUInteger)supportedInterfaceOrientations
 {
 return UIInterfaceOrientationMaskPortrait;
 }

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