IOS 7: code is used to solve the overall upward movement of views in the View Controller.

Source: Internet
Author: User

If you want to migrate your old iOS 6 app to iOS 7, you must pay attention to it. When your old app is running on iOS 7, all ViewController views are moved up as a whole, because iOS 7 sets the overall screen height (including the status bar and navigation bar) are used as the valid height of the View Controller. So your view is moved up and overlapped with the upper-layer status bar.

Of course, you can modify each View in Xcode and move them down to 20 pixels (Status Bar Height) or 64 pixels (status bar + navigation bar height ).

However, Apple has clearly considered this issue. They provided a new edgesForExtendedLayout attribute for ViewController In the iOS 7 SDK. If you set this attribute to UIRectEdgeNone, all the child views of viewController will be automatically adjusted, so that the effect on iOS 7 is exactly the same as that on iOS 6.

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 your later viewcontrollers will inherit from this DerivedViewController class.

However, unfortunately, our program still has a large number of iOS <7 users, and we cannot immediately discard the support for iOS 6. Both edgesForExtendedLayout and UIRectEdgeNone are valid only under iOS7. For iOS 6, I changed the above Code:

-(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

}

Use macro _ IPHONE_ OS _VERSION_MAX_ALLOWED to determine whether the deployment target is greater than 7.0.> 7.0 the new edgesForExtendedLayout API is used to move the subviews one by one in a stupid way, And the offset to be moved is automatically calculated based on the visible status of the status bar/navigation bar.

Note: If you have upgraded to Xcode5, set the Top Bar of the navigation controller to an "Opacque..." (not transparent) type to solve this problem.

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.