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.