IOS 7: Code solves the problem of Viewcontroller view overall move up

Source: Internet
Author: User

Resolve IOS7 View controller's view overall up to 20 pixels

When we create a new project using XCODE5, we find that the overall view of the app that was running on the iOS6 was moved up by 20 pixels, because IOS 7 took the entire screen height (including the status bar and navigation bar) as the effective height of the view controller, The controller view on the iOS6 is then overlapped with the status bar above.

On this issue, our workaround:

When Apple upgraded to iOS7, the controller view was taken into account as a whole, so a new property Edgesforextendedlayoutwas added to the view controller in the IOS7 SDK, set this property to Uirectedgenone, and then all of the Viewcontroller view moves up to 20 pixels in total.

For our app to be able to support both iOS6 and iOS7, for convenience, we can build a baseviewcontroller, rewrite its Viewdidload method, the code is as follows

- (void) viewdidload{[Super Viewdidload];#if__iphone_os_version_max_allowed >= 70000if([Self respondstoselector: @selector (edgesforextendedlayout)]) {Self.edgesforextendedlayout=Uiextendededgenone; }#else    floatBarheight =0; if(!isipad () &&![[UIApplication sharedapplication] Isstatusbarhidden]) {Barheight+=([[[UIApplication Sharedapplication]statusbarframe]). Size.Height; }    if(Self.navigationcontroller &&!self.navigationcontroller.navigationbarhidden) {barheight+=Self.navigationController.navigationBar.frame.size.height; }     for(UIView *viewinchself.view.subviews) {if([View Iskindofclass:[uiscrollviewclass]]) {View.frame= CGRectMake (view.frame.origin.x, View.frame.origin.y +barheight, View.frame.size.width, View.frame.size.height-barheight); } Else{view.frame= CGRectMake (view.frame.origin.x, VIEW.FRAME.ORIGIN.Y +barheight, View.frame.size.width, view.frame.size.height); }    }#endif}

Use the macro __iphone_os_version_max_allowed to determine if the current version is iOS7 and above. >7.0 uses the new edgesforextendedlayout API to traverse the sub-view subviews down one by one and automatically calculates the offset to move based on the visual state of the status bar/navigation bar.

Note: If you have upgraded to XCODE5, setting the Top Bar of the navigation controller to a "Opacque ..." (opaque) type resolves this issue.

The official has given the status bar a solution that is compatible with both IOS6 and iOS7. Integrated in Xcode5, as shown in: Deltas meaning you can understand as incremental. Relative increments, fill in 20.
Related Article

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.