IOS 7: Solving view overall move-up issues with code

Source: Internet
Author: User

If you're ready to migrate your old iOS 6 app to iOS 7, then you have to be aware of it. When your old app runs on an IOS 7 device, all Viewcontroller views move up as a whole, because IOS 7 takes the entire screen height (including the status bar and navigation bar) as a valid height for the view controller. So your view moves up and overlaps with the upper status bar.

You can of course modify each View in Xcode and move them down by 20 pixels (status bar height) or 64 pixels (status bar + navigation bar height).

But Apple has apparently taken this into account, and they have provided a edgesforextendedlayout new attribute for Viewcontroller in the IOS 7 SDK. If you set this property to Uirectedgenone, all child views of Viewcontroller are automatically adjusted so that you see the same effect as iOS 6 under iOS 7.

For convenience, you can extend a subclass for Uiviewcontroller and overwrite its Viewdidload method:

@implementation Derivedviewcontroller

-(void) viewdidload

{

[Superviewdidload];

if ([Selfrespondstoselector: @selector (edgesforextendedlayout)])

Self.edgesforextendedlayout = Uirectedgenone;

}

@end

Then all of your later viewcontroller inherit from this Derivedviewcontroller class.

Unfortunately, our program still has a large number of ios<7 users, and we can't immediately abandon support for IOS 6. Whether edgesforextendedlayout or Uirectedgenone, can only be effective under iOS7. For IOS 6, I modified the above code to:

-(void) viewdidload

{

[Superviewdidload];

#if__IPHONE_OS_VERSION_MAX_ALLOWED >= 70000

if ([Selfrespondstoselector: @selector (edgesforextendedlayout)])

Self.edgesforextendedlayout = Uirectedgenone;

#else

float barheight = 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 *viewin self.view.subviews) {

if ([View Iskindofclass:[uiscrollview class]]) {

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

}

Determine whether deployment target is >7.0 by macro __iphone_os_version_max_allowed. >7.0 uses the new Edgesforextendedlayout API, which is responsible for moving the subviews down one by one using a more clumsy method, 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.

IOS 7: Solving view overall move-up issues with code

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.