SoFunction
Updated on 2025-04-09

How to implement forced screen conversion, forced horizontal screen and forced vertical screen example code

This article introduces the example code of how iOS can implement forced screen conversion, forced horizontal screen and forced vertical screen. Share it with everyone

Today, when I was watching the video, the account was squeezed. If it was horizontal screen at that time, it would be necessary to force a vertical screen. It's a headache. I found many methods online, but finally solved it. O(∩_∩)O~

Forced horizontal screen:

[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];

Forced vertical screen:

[self interfaceOrientation:UIInterfaceOrientationPortrait];

Forced screen turn

- (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, it is because 0 1 The two parameters have been occupied by the selector and target.    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.