覆盖标签栏更改其高度

时间:2016-11-24 11:11:53

标签: ios objective-c swift uitabbarcontroller uitabbar

我之前尝试过搜索,但我找不到任何解决方案。 我想改变TabBar的高度来覆盖旧的TabBar。 我知道一个可能的解决方案是创建一个UIViewController子类并从头开始或者sottoclassare UITabBar但我想以这种方式尝试它并且我无法理解为什么不工作。

import UIKit

class NewTabBarController: UITabBarController {

let newTabBar : UITabBar = {

    let tmpView = UITabBar.init()
    tmpView.backgroundColor = UIColor.black
    tmpView.accessibilityLabel = "Nuova Tab Bar"
    tmpView.translatesAutoresizingMaskIntoConstraints = false

    return tmpView

}()

override var tabBar: UITabBar{


    return self.newTabBar

}

override func viewDidLoad() {
    super.viewDidLoad()

            self.view.addSubview(newTabBar)
    self.newTabBar.heightAnchor.constraint(equalToConstant: 100).isActive = true
    self.newTabBar.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
    self.newTabBar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
    self.newTabBar.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

    // Do any additional setup after loading the view, typically from a nib.
}

我尝试了各种解决方案。 使用框架,约束,但没有这些工作。 有了约束我得到了这个错误

 *** Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Cannot modify constraints for
UITabBar managed by a controller'

改为使用框架:

var newTabBar : UITabBar = {

    let tmpView = UITabBar.init()
    tmpView.backgroundColor = UIColor.black
    tmpView.accessibilityLabel = "New Bar"
    return tmpView

}()

override var tabBar: UITabBar{


    return self.newTabBar

}

override func viewDidLoad() {
    super.viewDidLoad()

    self.newTabBar.frame = CGRect.init(x: 0, y: self.view.frame.height - 110, width: self.view.frame.width, height: 110)
    // Do any additional setup after loading the view, typically from a nib.
}

它没有出现。 如果我将newTabBar添加为子视图,它会出现,但第一个不会消失。

1 个答案:

答案 0 :(得分:0)

Swift 3

中试用此代码
let CustomTabBarHeight = 100 // Set your custom tabBar height here

override func viewWillLayoutSubviews() {
    var customTabFrame = self.tabBarController?.tabBar.frame
    customTabFrame?.size.height = CGFloat(CustomTabBarHeight)
    customTabFrame?.origin.y = self.view.frame.size.height - CGFloat(CustomTabBarHeight)
    self.tabBarController?.tabBar.frame = customTabFrame!
    tabBarController?.tabBar.barTintColor = .black
    tabBarController?.tabBar.tintColor = .white
}