我搜索和搜索,到目前为止我只找到了答案:
我需要知道:是否有可能决定状态栏的通用颜色,它会出现在应用程序的任何位置?
感谢您的帮助
答案 0 :(得分:0)
实现此目的的方法是创建一个具有正确位置和尺寸的UIView,然后直接将其添加到UIWindow:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var statusBarColorView: UIView?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window?.makeKeyAndVisible()
let v = UIView(frame: CGRect(x: 0, y: 0, width: window?.frame.size.width ?? 1024, height: 20))
v.backgroundColor = UIColor.white
v.translatesAutoresizingMaskIntoConstraints = false
window?.addSubview(v)
window?.addConstraints(NSLayoutConstraint.constraints(
withVisualFormat: "V:|-0-[v(==20)]",
options: [],
metrics: nil,
views: ["v": v]))
window?.addConstraints(NSLayoutConstraint.constraints(
withVisualFormat: "H:|-0-[v]-0-|",
options: [],
metrics: nil,
views: ["v": v]))
return true
}
这种方法的问题是你需要根据设备方向/当前显示的视图控制器自己隐藏/显示它。
更改颜色将如下所示:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.statusBarColorView?.backgroundColor = UIColor.blue
}
}
您肯定希望根据颜色自定义UIStatusBarStyle,您可以通过为Info.plist中的密钥YES
选择UIViewControllerBasedStatusBarAppearance
来实现此目的