UITabBar不会改变其背景的颜色

时间:2015-12-22 19:35:21

标签: ios swift uitabbar

简易代码

class PreferencesTabBar: UITabBar {

    override func drawRect(rect: CGRect) {
        super.drawRect(rect)
        self.backgroundColor = UIColor(red: 166.0/255.0, green: 142.0/255.0, blue: 83.0/255.0, alpha: 0.5)
    }

}

我将IB中的UITabBar类设置为我的自定义类,但背景并不是我想要的颜色,而是变得完全透明,就像没有UITabBar一样所有(当然除了UITabBarItems,我仍然可以看到它们)

1 个答案:

答案 0 :(得分:0)

一种解决方案是通过从标签栏创建@IBOutlet(按住Ctrl键拖动),从包含标签栏的视图控制器设置标签栏的背景。

// ViewController.swift
class ViewController: UIViewController {

    @IBOutlet weak var myTabBar: UITabBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        myTabBar.backgroundColor = UIColor(red: 166.0/255.0, green: 142.0/255.0, blue: 83.0/255.0, alpha: 0.5)
    }

    // ...

}