Keep iPhone rotation in landscape?

2019-08-30 04:45发布

I'm making an app that is only in landscape mode but I've found that when I rotate the device, the app auto-rotates to that orientation. I specified in the project summary that I wanted only "Landscape Left" and then in each view controller I put

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

Although the app starts in landscape when I click rotate right or left, the simulator goes into portrait as it should but then the app auto-rotates as well. How can I get the app to stay in landscape even when the device is rotated?

3条回答
孤傲高冷的网名
2楼-- · 2019-08-30 05:23

In your info.plist, you need to set the key for UISupportedInterfaceOrientations, As seen below.:

enter image description here

This limit's my app to run only in Landscape mode, in addition to your shouldAutorotateToInterfaceOrientation: method. If you're supporting Landscape Left/Right. Your method should look like this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return  (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-08-30 05:26

I think you have misunderstood what the shouldAutorotateToInterfaceOrientation: is for. It is not asking you "What orientations do you support?", it is asking you "Do you support this interface orientation?". So your answer should be either YES or NO.

It will ask you this each time before it decides to change orientation, so you can change your mind and sometimes support it, sometimes not (if you really want).

For example, to support all orientations:

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

...to support only landscape orientations:

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

...to support only landscape-left (as you want):

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return(interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
查看更多
Luminary・发光体
4楼-- · 2019-08-30 05:28

In addition to what you did, Instead of your shouldAutorotateToInterfaceOrientation function, use the following

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
查看更多
登录 后发表回答