2016-11-24 91 views
0

我試圖在之前進行搜索,但找不到任何解決方案來解決我的問題。 我想更改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作爲子視圖,它會出現,但第一個不會消失。

回答

0

試一下這個code.tested在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 
} 
+0

是,它爲我工作太多,但我要問:「爲什麼我不能使用覆蓋getter方法做到這一點」 。在我看來,它應該是正確的,因爲我超越了超級的屬性。 – ndPPPhz