Transferred from: http://www.molotang.com/articles/1530.html
Then the previous write touch event, this time take the opportunity to collate the iOS horizontal screen and vertical screen flip direction support, that is, interfaceorientation related content .
Recently made a page, initially did not think much of the case of orientation, when it is embedded in an ipad using a horizontal screen (Landscape) in the application, it will only appear on the left side of the screen, and does not seem to show the full, this ... It's ugly! Feel it from the heart to do so sorry Apple design concept! I'm sorry Joe Don ...
Change! It is time to understand the support for the horizontal screen (Landscape) and the vertical screen (Portrait) of iOS apps in Apple development.
0. Application-level configuration
Everyone (especially those with iOS development experience) should know that there are four graphs (or 4 checkboxes) in the project Configuration General tab of Xcode project, which identifies support for four kinds of interfaceorientation. Portrait, Portraitupsidedown, Landscapeleft and Landscaperight, respectively.
corresponding, the info page in the Xcode Project project configuration, is actually info.plist, with a record entry for 4 kinds of orientation.
The two are the same.
1. Window-level control
After iOS6.0, there is one more method declaration in Uiapplicationdelegate:
| 1 |
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window |
That is, for specific application and specific windows, which interfaceorientation we need to support, which can be customized by implementing this method.
The return value is an unsigned integer that can actually be used with a well-defined enumeration value:
| 123456789 |
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) { UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait), UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft), UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight), UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown), UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown), UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),}; |
For Uiapplicationdelegate, this method declares that in most cases application is the current application, and window usually has only one. So basically through window to the horizontal screen vertical screen interfaceorientation control equivalent to the overall.
2. Control at the controller level
The old version of iOS has such a method:
| 1 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0); |
That is, whether the customization can be rotated to a specific interfaceorientation.
After IOS6, 2 new methods were introduced to accomplish this task:
| 12 |
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0); |
It can be seen that two and together are the complete process of the original task. Among them, the approximate way of judging is to first execute the former, to determine whether it can be rotated, if yes, then make decisions based on whether or not to support a particular interfaceorientation.
3. Make specific Viewcontroller adhere to specific interfaceorientation
IOS6 also provides a way to keep your controller stubborn by sticking to a particular interfaceorientation:
| 1 |
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0); |
This is called persistence, hehe!
Of course, there is another set of enumerators that you can go to UIApplication.h to see the definition.
4. interfaceorientation for current screen orientation
There are 3 ways to get to "current Interfaceorientation":
- Controller.interfaceorientation, obtaining the direction of a specific controller
- [[UIApplication sharedapplication] statusbarorientation] Get status bar related direction
- [[Uidevice Currentdevice] orientation] get the direction of the current device
For specific differences, see StackOverflow's Questions and answers:
Http://stackoverflow.com/questions/7968451/different-ways-of-getting-current-interface-orientation
5. Support for container controller
Above the interfaceorientation direction of the acquisition and support Configuration said, it seems that there is no problem. Are there any special cases?
There are headaches when you use Tabbarcontroller and Navigationcontroller as described above.
method is not no, the more popular one is--inherit the realization.
(Supplementary: Delegate can be controlled after iOS7)
The contents of iOS Interface Orientation screen orientation are organized here, and you are welcome to speak to crossing.
0Related articles:
- December 20, 2013-iOS app status and its switchover (life cycle)
- December 18, 2013--iOS app evokes and customizes URL Scheme
- February 22, 2014-Background and multitasking for iOS
- May 11, 2014-use of block in iOS
This entry was posted in iOS, iOS development basics, Computer technology catalog, affixed with Interfaceorientation, iOS, Landscape, Portrait,uiapplicationdelegate, Uiviewcontroller, UIWindow, screen orientation label. Add a pinned link to your Favorites folder.
Horizontal screen (Landscape) and vertical screen (Portrait) for iOS interfaceorientation