本文转载至:http://blog.csdn.net/pucker/article/details/12112105
苹果终于发布了iOS 7正式版,大批的用户都已经纷纷进行了升级。如果App是由Xcode 4.6或者更早版本生成,iOS 7系统会使用兼容模式运行该App,以便尽可能保持原有外观。但是,当使用Xcode 5重新编译App源代码时,此时会使用iOS 7 SDK来进行编译链接。(注意在使用Xcode 5打开旧项目之前备份项目源代码,因为Xcode 5会升级项目中的资源文件,且无法再使用旧版本的Xcode打开。)由于iOS 7 SDK较早期版本的SDK改动较大,因此App的界面也会出现种种问题,其中最明显的问题就是状态栏与导航栏的显示问题。
iOS 6:
iOS7:
当未使用导航栏时,上面的截图对比了在iOS 6与iOS 7上的显示情况。iOS 6中的状态栏不透明,视图控制器的主视图原点在状态栏下面。而iOS 7的状态栏背景色变为透明色,视图控制器的主视图原点在屏幕左上角,即状态栏显示在主视图之上,透过状态栏可以显示视图的内容。
iOS 7提供了两种状态栏的样式,用于控制状态栏文字的颜色。
- typedef NS_ENUM(NSInteger, UIStatusBarStyle)
- {
- UIStatusBarStyleDefault = 0, // Dark content, for use on light backgrounds
- UIStatusBarStyleLightContent NS_ENUM_AVAILABLE_IOS(7_0) = 1, // Light content, for use on dark backgrounds
- UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1,
- UIStatusBarStyleBlackOpaque NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,
- };
typedef NS_ENUM(NSInteger, UIStatusBarStyle)
{
UIStatusBarStyleDefault = 0, // Dark content, for use on light backgrounds
UIStatusBarStyleLightContent NS_ENUM_AVAILABLE_IOS(7_0) = 1, // Light content, for use on dark backgrounds UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1,
UIStatusBarStyleBlackOpaque NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,
};
如果状态栏背景为浅色,应选用黑色字样式(UIStatusBarStyleLightContent);如果背景为深色,则选用白色字样式(UIStatusBarStyleDefault,默认值)。
iOS 7的UIViewController类里添加了几个新的方法,用于控制状态栏。
- // These methods control the attributes of the status bar when this view controller is shown. They can be overridden in view controller subclasses to return the desired status bar attributes.
- - (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0); // Defaults to UIStatusBarStyleDefault
- - (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO
- // This should be called whenever the return values for the view controller's status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the animation block.
- - (void)setNeedsStatusBarAppearanceUpdate NS_AVAILABLE_IOS(7_0);
// These methods control the attributes of the status bar when this view controller is shown. They can be overridden in view controller subclasses to return the desired status bar attributes.
- (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0); // Defaults to UIStatusBarStyleDefault
- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO // This should be called whenever the return values for the view controller's status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the animation block.
- (void)setNeedsStatusBarAppearanceUpdate NS_AVAILABLE_IOS(7_0);
preferredStatusBarStyle方法用于指定当前视图控制器的状态栏样式,prefersStatusBarHidden方法指定是否隐藏状态栏。当修改了状态栏的字体颜色、显示或隐藏时,调用setNeedsStatusBarAppearanceUpdate方法告知系统需要刷新状态栏。