Preface
You may often encounter screen rotation problems in iOS program development, such as hoping that the specified page will rotate differently, but since the method provided by the system is the global method of the navigation controller, this requirement cannot be achieved at will. The general solution is to inherit UINavrgationViewController and rewrite the relevant methods of this class. Although this can also solve the problem, at least two unnecessary files and a lot of code are generated during the rewriting process, which is obviously not what we want. Here is a lower-level method to solve this problem.
Basic Principles
Dynamically change the global method of UINavrgationViewController, replace the supportedInterfaceOrientations, shouldAutorotate methods and navigation controller object methods that we rewritten ourselves.
Preparation
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 configuration of the screen rotation direction.
//Get the rotation support method of the current view controllerMethod selfMtihod = class_getInstanceMethod([self class], @selector(shouldAutorotate)); //Get the rotation support method of the current navigation controllerMethod navr = class_getInstanceMethod([ class], @selector(shouldAutorotate)); //Exchange methodmethod_exchangeImplementations(selfMtihod, navr); //The following is the sameMethod selfOrientation = class_getInstanceMethod([self class], @selector(supportedInterfaceOrientations)); Method navrOrientation = class_getInstanceMethod([ class], @selector(supportedInterfaceOrientations)); method_exchangeImplementations(selfOrientation, navrOrientation);
How to use
Rewrite supportedInterfaceOrientations and shouldAutorotate in the above parent class to indicate the default screen rotation related attributes.
In each subsequent subclass of the attempted controller, the supportedInterfaceOrientations and shouldAutorotate methods can be rewritten to complete the requirements for specifying the view controller direction.
The above is a detailed explanation of the Runtime method to solve the screen rotation problem introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support for my website!