当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS开发技巧之状态栏字体颜色的设置方法

iOS开发技巧之状态栏字体颜色的设置方法

2019年07月23日  | 移动技术网移动技术  | 我要评论

状态栏的字体为黑色:uistatusbarstyledefault

状态栏的字体为白色:uistatusbarstylelightcontent

一、在info.plist中,将view controller-based status bar appearance设为no

状态栏字体的颜色只由下面的属性设定,默认为白色:

// default is uistatusbarstyledefault
[uiapplication sharedapplication].statusbarstyle

解决个别vc中状态栏字体颜色不同的办法

1、在info.plist中,将view controller-based status bar appearance设为no.

2、在app delegate中:

[uiapplication sharedapplication].statusbarstyle = uistatusbarstylelightcontent;

3、在个别状态栏字体颜色不一样的vc中

-(void)viewwillappear:(bool)animated{
[uiapplication sharedapplication].statusbarstyle = uistatusbarstyledefault;
}

-(void)viewwilldisappear:(bool)animated
{
[super viewwilldisappear:animated];
[uiapplication sharedapplication].statusbarstyle = uistatusbarstylelightcontent;
}

二、在info.plist中,将view controller-based status bar appearance设为yes,或者没有设置。

view controller-based status bar appearance的默认值就是yes。

如果view controller-based status bar appearance为yes。

[uiapplication sharedapplication].statusbarstyle 无效。

用下面的方法:

1、在vc中重写vc的preferredstatusbarstyle方法。

-(uistatusbarstyle)preferredstatusbarstyle
{

return uistatusbarstyledefault;
}

2、在viewdidload中调用: [self setneedsstatusbarappearanceupdate];

但是,当vc在nav中时,上面方法没用,vc中的preferredstatusbarstyle方法根本不用被调用。

原因是, [self setneedsstatusbarappearanceupdate]发出后,

只会调用navigation controller中的preferredstatusbarstyle方法,

vc中的preferredstatusbarstyley方法跟本不会被调用。

解决办法有两个:

方法一:

设置navbar的barstyle 属性会影响status bar 的字体和背景色。如下。

//status bar的字体为白色

//导航栏的背景色是黑色。

self.navigationcontroller.navigationbar.barstyle = uibarstyleblack;

//status bar的字体为黑色

//导航栏的背景色是白色,状态栏的背景色也是白色。

//self.navigationcontroller.navigationbar.barstyle = uibarstyledefault;

方法二:

自定义一个nav bar的子类,在这个子类中重写preferredstatusbarstyle方法:

mynav* nav = [[mynav alloc] initwithrootviewcontroller:vc];

self.window.rootviewcontroller = nav;

@implementation mynav

- (uistatusbarstyle)preferredstatusbarstyle

{

uiviewcontroller* topvc = self.topviewcontroller;

return [topvc preferredstatusbarstyle];

}

附:修改状态栏的背景颜色 (牵扯到uiwindow的层级关系)

/*改变状态栏的背景颜色,因为状态栏的层级比较高,所以按照如下添加就可以出来效果*/
uiview* stateview = [[uiview alloc] initwithframe:cgrectmake(0, -20, screen_width, 20)];
[self.navigationcontroller.navigationbar addsubview:stateview];
stateview.backgroundcolor = [uicolor purplecolor];

备注: 因为在oc中,都应该知道uiwindow有三个层级,如下:

uikit_extern const uiwindowlevel uiwindowlevelnormal;
uikit_extern const uiwindowlevel uiwindowlevelalert;
uikit_extern const uiwindowlevel uiwindowlevelstatusbar

它们层级的优先级的值分别对应的是:

uiwindowlevelnormal: 0

uiwindowlevelalert: 1000

uiwindowlevelstatusbar:2000

(而且uialertview的层级优先级为1996,uiactionsheet的层级优先级为2001)
优先级

总结:

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网