Example code of how IOS implements forced screen rotation, forced horizontal screen and forced vertical screen

Time:2022-5-9

This paper introduces the example code of IOS how to realize forced screen rotation, forced horizontal screen and forced vertical screen, which can be shared with you

In today’s project, when I was watching a video, my account was crowded. If it was horizontal, I need to force the vertical screen. It’s a headache. I found a lot of methods online and finally solved it. O(∩_∩)O~

Forced horizontal screen:


[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];

Force vertical screen:


[self interfaceOrientation:UIInterfaceOrientationPortrait];

Forced screen rotation

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
  if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
    SEL selector = NSSelectorFromString(@"setOrientation:");
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[UIDevice currentDevice]];
    int val = orientation;
    //Starting from 2 is because 0 and 1 parameters have been occupied by selector and target
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
  }
}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

How do computer browsers switch between different kernel modes to browse web pages

How to switch between different kernel modes to browse web pages: 1. Open the web page that only supports IE kernel, and when logging in, it will prompt “only supports IE6.0 and above”; 2. Click the lightning icon on the far right of the address bar to switch to compatibility mode (suitable for websites that […]