来自 UITabBar 的奇怪行为,其中背景颜色仅显示一个选项卡
Odd behavior from UITabBar where background color only shows for one tab
我正在使用 Swift 3
并研究了各种方法来设置 UITabBar
的 backgroundColor
颜色。最简单的方法就是把AppDelegate
中的didFinishLaunchingWithOptions
中的属性改掉。当我尝试这样做时,UITabBar
以标准颜色启动,这不是我想要的结果。
但是,当我转到下一个选项卡时,UITabBar
颜色变为我想要的颜色。
在此之后,我尝试 subclass
我的 UITabBarController
并尝试在 viewDidLoad
和 viewDidLayoutSubViews
中设置背景颜色。所有这三种尝试都表现出完全相同的行为 - 即 UITabBar
以标准颜色启动,并且仅在我切换到下一个选项卡时才更改颜色。
这是我的代码:
应用程序委托
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
-> Bool {
UITabBar.appearance().backgroundColor = UIColor.red
return true
}
viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.backgroundColor = UIColor.red
}
viewDidLayoutSubviews
override func viewDidLayoutSubviews() {
self.tabBar.backgroundColor = UIColor.red
}
我的问题如下:
1) 我可以将其作为约束问题消除吗?我没有为 UITabBar
修改或设置任何约束
2) 我有一个启动屏幕,用户可以在其中选择确认内容,然后他们 segued
到 UITabBarController
。这可能是问题的一部分吗?
将它放入我的应用程序委托中,我得到了一个红色的标签栏。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let tabBarVC = UITabBarController()
tabBarVC.viewControllers = [FirstViewController(), SecondViewController()]
tabBarVC.tabBar.backgroundColor = UIColor.red
window?.rootViewController = tabBarVC
window?.makeKeyAndVisible()
return true
}
更改标签栏颜色的基本方法不是像您现在那样设置它的 backgroundColor
,而是设置它的 barTintColor
。 (要进行更复杂的控制,请使用彩色 backgroundImage
。)
我正在使用 Swift 3
并研究了各种方法来设置 UITabBar
的 backgroundColor
颜色。最简单的方法就是把AppDelegate
中的didFinishLaunchingWithOptions
中的属性改掉。当我尝试这样做时,UITabBar
以标准颜色启动,这不是我想要的结果。
但是,当我转到下一个选项卡时,UITabBar
颜色变为我想要的颜色。
在此之后,我尝试 subclass
我的 UITabBarController
并尝试在 viewDidLoad
和 viewDidLayoutSubViews
中设置背景颜色。所有这三种尝试都表现出完全相同的行为 - 即 UITabBar
以标准颜色启动,并且仅在我切换到下一个选项卡时才更改颜色。
这是我的代码:
应用程序委托
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
-> Bool {
UITabBar.appearance().backgroundColor = UIColor.red
return true
}
viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.backgroundColor = UIColor.red
}
viewDidLayoutSubviews
override func viewDidLayoutSubviews() {
self.tabBar.backgroundColor = UIColor.red
}
我的问题如下:
1) 我可以将其作为约束问题消除吗?我没有为 UITabBar
2) 我有一个启动屏幕,用户可以在其中选择确认内容,然后他们 segued
到 UITabBarController
。这可能是问题的一部分吗?
将它放入我的应用程序委托中,我得到了一个红色的标签栏。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let tabBarVC = UITabBarController()
tabBarVC.viewControllers = [FirstViewController(), SecondViewController()]
tabBarVC.tabBar.backgroundColor = UIColor.red
window?.rootViewController = tabBarVC
window?.makeKeyAndVisible()
return true
}
更改标签栏颜色的基本方法不是像您现在那样设置它的 backgroundColor
,而是设置它的 barTintColor
。 (要进行更复杂的控制,请使用彩色 backgroundImage
。)