【问题标题】:Customising the UITabBar.appearance in each View Controller [Swift]在每个视图控制器中自定义 UITabBar.appearance [Swift]
【发布时间】:2016-11-16 04:33:12
【问题描述】:

我正在尝试将标签栏设置为在每个视图控制器上具有不同的背景图像。

class CharacterVC: UIViewController {

var tabBarApparence = UITabBar.appearance()

override func viewDidLoad() {
    super.viewDidLoad()

    tabBarApparence.backgroundImage = UIImage(named: "BlueTB") //Loaded from Image Asset
}

这工作正常并在该视图中将其更改为蓝色,但是当我转到下一个视图时,它保持蓝色并且不会更改为我使用此代码编程的红色:

class AnonVC: UIViewController {

var tabBarApparence = UITabBar.appearance()

override func viewDidLoad() {
    super.viewDidLoad()
    tabBarApparence.backgroundImage = UIImage(named: "RedTabBar")
    // addtional code here
}

我有另外 2 个视图控制器,一个应该显示图像的绿色版本,另一个显示图像的紫色版本。

有什么建议可以解决这个问题吗?

【问题讨论】:

    标签: ios swift uitabbar tabbar


    【解决方案1】:

    如果你想在视图控制器中改变 TabBar 的外观是很容易的。您可以在函数 viewDidLoad 或 viewWillAppear 中执行此操作。代码如下:

    // Set color of titles and icons in tabBar
    self.tabBarController?.tabBar.tintColor = UIColor.redColor()
    // Set color of background tabBar
    self.tabBarController?.tabBar.barTintColor = UIColor.blueColor()
    

    【讨论】:

    • 当我去每个视图时都有效,谢谢!但是,当我返回上一个视图时,它是否与上一个视图相同?知道我怎样才能拥有它,以便它切换到为该视图设置的那个吗?
    • 等等!我得到了它与 viewWillAppear() 的@Igor 建议一起工作,谢谢你们!
    【解决方案2】:
    1. 将代码替换为viewWillAppear()
    2. 最好将 backgroundImage 设置为 tabBar,而不是 UITabBar.appearance()

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 2023-03-27
      • 2016-09-27
      • 1970-01-01
      • 2023-03-25
      • 2017-03-17
      • 2011-05-15
      • 1970-01-01
      相关资源
      最近更新 更多