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

╄→尐↘猪︶ㄣ 提交于 2019-12-10 17:08:55

问题


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


来源:https://stackoverflow.com/questions/11429248/how-lock-screen-in-ios-for-it-to-have-only-portrait-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!