English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of Runtime Solution to Screen Rotation Problem in Android (Recommended)

Preface

You may often encounter screen rotation issues in iOS program development, such as the need for specified pages to have different screen rotations, but since the system provides methods are global methods of navigation controllers, it is not possible to arbitrarily meet this requirement. The general solution is to inherit UINavrgationViewController and rewrite the relevant methods of the class. Although this can also solve the problem, at least two extra files and a lot of code are generated in the process of rewriting, which is obviously not what we want. Below is a more fundamental method to solve this problem.

Basic principle

Dynamically change the global method of UINavrgationViewController, and replace the methods of our rewritten supportedInterfaceOrientations, shouldAutorotate, and navigation controller object.

Preparation work

Configure project support direction

Code implementation

Write the following method in the viewDidLoad method of the parent class of all view controllers to complete the screen rotation direction configuration.

//Obtain the current view controller's rotation support method
Method selfMtihod = class_getInstanceMethod([self class], @selector(shouldAutorotate));
//Obtain the current navigation controller's rotation support method
Method navr = class_getInstanceMethod([self.navigationController class], @selector(shouldAutorotate));
//Exchange methods
method_exchangeImplementations(selfMethod, navr);
//The same as above
Method selfOrientation = class_getInstanceMethod([self class], @selector(supportedInterfaceOrientations));
Method navrOrientation = class_getInstanceMethod([self.navigationController class], @selector(supportedInterfaceOrientations));
method_exchangeImplementations(selfOrientation, navrOrientation);

Usage Method

Overwrite supportedInterfaceOrientations and shouldAutorotate in the above superclass to indicate the default screen rotation-related properties.

In each subclass of the controller to be controlled later, you can overwrite the supportedInterfaceOrientations and shouldAutorotate methods to complete the specified view controller orientation requirements.

The above-mentioned is a detailed explanation of the method of solving the screen rotation problem of the Runtime introduced by the editor for everyone, hoping it will be helpful to everyone. If you have any questions, please leave me a message, and the editor will reply to everyone in time. Here we also thank everyone for their support of the Yelling Tutorial website!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright. It has not been manually edited and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like