您的位置:首页 > 移动开发 > IOS开发

ios8 statusBar hidden and show

2015-09-25 15:09 441 查看

iOS8 statusBar hidden and show

写博客的原因:

从iOS7开始到iOS9,代码中一共不下6次用到,每次都要从网上搜好半天,但是依然不得要领,每次都要重复造车轮,相对来说,自己写一篇详细的笔记,以后使用可能会更方便。

三石的博客上解释的很明确,statusBar的显示与隐藏,从思路着手,我们首先应该想到UIViewController、UINavgationController、UIWindow、UIApplication 这几个类,之后查看这些类的API,看看有没有相关的方法,然后进行尝试。

需求 全局隐藏statusBar

查看UIApplication的API

[code]    // Setting the statusBarStyle does nothing if your application is using the default UIViewController-based status bar   system.
    @property(readwrite, nonatomic) UIStatusBarStyle statusBarStyle NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController     preferredStatusBarStyle]");
    - (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle animated:(BOOL)animated NS_DEPRECATED_IOS(2_0, 9_0, "Use -   [UIViewController preferredStatusBarStyle]");
    // Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.
    @property(readwrite, nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden NS_DEPRECATED_IOS(2_0, 9_0, "Use -    [UIViewController prefersStatusBarHidden]");
    - (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_DEPRECATED_IOS(3_2, 9_0, "Use -    [UIViewController prefersStatusBarHidden]");


通过阅读注释
// Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.
查找 UIViewController-based status bar system.设置地方,可知需要在plist文件中设置 ,测试,statusBar成功影藏请使用含有一个viewCOntroller的项目进行测试,防止其他地方干扰

需求增加 设定某个viewController statusBarShow且content 为白色

设定 plist文件中 UIViewController-based status bar system = YES;

设定 [[UIApplication sharedApplication] setStatusBarHidden:NO];

查看UIViewController API

[code]// 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_***AILABLE_IOS(7_0); // Defaults to UIStatusBarStyleDefault
- (BOOL)prefersStatusBarHidden NS_***AILABLE_IOS(7_0); // Defaults to NO
// Override to return the type of animation that should be used for status bar changes for this view controller. This currently only affects changes to prefersStatusBarHidden.
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation NS_***AILABLE_IOS(7_0); // Defaults to UIStatusBarAnimationFade

// 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_***AILABLE_IOS(7_0);


仔细阅读API及注释发现,ViewController 可以自己设定特定的statusBarStyle 及hidden show 测试发现ok

引入到工程中发现statusBarStyle没有实现

貌似中间漏掉了什么,对,UINavigationController,xcode6.4之前建立的项目,要设置UINavigationController的NavgationBar的barStyle为黑白,才能实现,正如苹果API所说:

UIStatusBarStyleDefault                                     = 0, // Dark content, for use on light backgrounds 

UIStatusBarStyleLightContent     NS_ENUM_***AILABLE_IOS(7_0) = 1, // Light content, for use on dark backgrounds


实现的时候发现个问题:

屏幕开始时,statusBar由黑色变为白色,中间有个变化的过程,通过修改infoPlist中的Status bar style 为Transparent black style (alpha of 0.5),变化消失

发现问题,先通过查找苹果的API,检查实现是否遵循苹果的要求,不要一味的查网上的资源
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: