标签栏不显示设置的backgroundColor和UIView不显示

时间:2018-07-25 03:56:35

标签: ios swift

我设置了一个tabBar,使其显示常量mainBlue的backgroundColor,应该是蓝色。

let mainBlue = UIColor(red: 93/255, green: 135/255, blue: 186/255, alpha: 1)

每当我关闭半透明度时,标签栏就会完全变成白色。

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(topView)
    setupTabBar()

    topView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    topView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    topView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    topView.heightAnchor.constraint(equalToConstant: 100).isActive = true
}

let topView: UIView = {
   let view = UIView()
    view.backgroundColor = secondaryBlue

    return view
}()

func setupTabBar() {
    let barController = BarMainViewController()
    barController.tabBarItem.image = #imageLiteral(resourceName: "Bar").withRenderingMode(.alwaysOriginal)
    barController.tabBarItem.selectedImage = #imageLiteral(resourceName: "BarPressed").withRenderingMode(.alwaysOriginal)

    let requestController = RequestMainViewController()
    requestController.tabBarItem.image = #imageLiteral(resourceName: "Request").withRenderingMode(.alwaysOriginal)
    requestController.tabBarItem.selectedImage = #imageLiteral(resourceName: "RequestPressed").withRenderingMode(.alwaysOriginal)

    let userController = UserMainViewController()
    userController.tabBarItem.image = #imageLiteral(resourceName: "User").withRenderingMode(.alwaysOriginal)
    userController.tabBarItem.selectedImage = #imageLiteral(resourceName: "UserPressed").withRenderingMode(.alwaysOriginal)

    tabBar.backgroundColor = mainBlue
    tabBar.isTranslucent = false

    viewControllers = [barController, requestController, userController]

    guard let items = tabBar.items else { return }

    for item in items {
        item.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0)
    }
}

此外,topView不在模拟器中显示。顶部只有一个黑条。模拟器中的红色背景是对我以编程方式嵌入的containerView的填充。

这里是screenshot of the simulator

0 个答案:

没有答案