使用不同的颜色设置导航栏和状态栏 [英] Set the navigation bar and status bar with different colors

查看:99
本文介绍了使用不同的颜色设置导航栏和状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift制作应用程序,我希望状态栏和导航栏有不同的颜色,例如谷歌翻译应用程序或iOS上的谷歌音乐应用程序。

I´m making an app with Swift and I want the status bar and the navigation bar to have different colors, like the google translate app or the google music app on iOS.

我已经知道状态栏和导航栏的颜色不能单独设置,但是有什么类型的技巧吗?

I already know that the colors of the status bar and the navigation bar can´t be set separately, but is there any type of trick to do it?

推荐答案

一种简单的方法是在导航栏顶部添加一个与状态栏完全相同的视图。

A simple way to do this would be to add a view on top of the navigation bar that is the exact size of the status bar.

例如,要使状态栏变为红色,可以将以下内容添加到viewDidLoad方法中:

For instance, to make the status bar become red, you could add the following to your viewDidLoad method:

let statusBarHeight = CGFloat(20)
let colorView = UIView(frame: CGRectMake(0, -statusBarHeight, self.view.bounds.width, statusBarHeight))
colorView.backgroundColor = UIColor.redColor()
self.navigationController?.navigationBar.addSubview(colorView)

这篇关于使用不同的颜色设置导航栏和状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆