Implementation of auto-rotation of Uialertview with iOS system

Source: Internet
Author: User

Here is the main analysis of the implementation of several key functions of Uialertview :

    • Rotate as the screen of the device rotates;
    • The alert popup is implemented using UIWindow, which means that you do not have to rely on the UIView that is currently displayed at the front.

Implement source code reference: Http://git.oschina.net/shede333/BMAlertHud

Using UIWindow

Pop Up an alert box, preferably create a new UIWindow,

    • Put window.windowlevel = Uiwindowlevelalert, so that it can be displayed at the front;
    • Called [window makeKeyAndVisible] , the window can be displayed,
    • Call [window resignKeyWindow] , the window disappears to show the previous window,
    • Try not to set directly [window addSubview:alertView] to the window to add UIView, screen rotation will be problematic, because when the screen is rotated, the app calls Key-window, Window.rootviewcontroller, So, in the new one UIWindow is, it is best to set a rootviewcontroller for window, and then paste the alertview you need to display on the Rootviewcontroller.view , This makes it easy to handle screen rotation issues later.
Screen Rotation implementation

Here is the iOS6 and the implementation of the higher system version; Uiviewcontroller Two system functions

    • - (BOOL)shouldAutorotate: rotation is supported, default return YES
    • - (NSUInteger)supportedInterfaceOrientations: Which direction rotation is supported (4 directions, set using enumeration values UIInterfaceOrientationMask );
      Only the above shouldAutorotate returns Yes, the function is called; The default value on the ipad is UIInterfaceOrientationMaskAll ;
      The default value for iphone isUIInterfaceOrientationMaskAllButUpsideDown

Rewrite the above two functions to achieve a screen rotation or not.

When the screen rotates, the control position is customized

Uiviewcontroller Two system functions, but was abandoned on iOS8, mainly implemented: when the screen rotation, sub-view layout changes, here to distinguish:

    • - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
      This function is placed in the animation-block of the screen rotation animation, so, any changes to the sub-view here, will animate the implementation of the screen rotation, to customize the location of the child view, it is implemented here.

    • - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
      Rewrite this function, the implementation of the code inside does not animate, it is suggested that the screen rotation, you need to be similar to the forbidden view of touch events, stop music playback and other actions here;

  • Ios4,ios5 IOS6 screen rotation orientation (automatic rotation, manual rotation, compatible with IOS6 system)
Adaptation issues

Here are the differences between iOS6, 7, and IOS8:

size of screen screens:[[UIScreen mainScreen] bounds].size

In IOS6, 7 with the rotation of the screen, the size of screens, and the length of the view of the associated Uiviewcontroller is constant, that is, the height is always greater than the width;
On the iOS8, as the screen rotates, the size of screens and the length of the view of the associated Uiviewcontroller are changed ,

Therefore, you need to set the Alertview with the screen rotation and rotation, considering the adaptation of IOS6, 7, 8, you need to do the following settings:

    • - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)durationThis function is implemented: when the screen rotates, set the position of the sub-view, Note that the size of screens on the iOS8 will change.

In fact, iOS8 on the screen rotation support is very good, we only need to autoresizingMask handle the view set, we will achieve the effect we need;
But in IOS6, 7, our team sub-view changes, most of the work needs to be in the - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration active setting sub-view;

Reference

Implement source code reference: Http://git.oschina.net/shede333/BMAlertHud

Implementation of auto-rotation of Uialertview with iOS system

Implementation of the Uialertview automatic rotation of the iOS system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.