首页 > 解决方案 > 使用自定义高度制作工具栏 | 斯威夫特 5

问题描述

本质上,我有以下工具栏,当前设置为在左侧和右侧包含两个按钮:

//Select All Toolbar
self.navigationController?.isToolbarHidden = false
self.navigationController?.toolbar.barTintColor = UIColor(named: "tabBarColor")
//self.navigationController?.toolbar.barTintColor = UIColor.white
self.navigationController?.toolbar.sizeToFit() // without this line it doesn't work
self.navigationController?.toolbar.tintColor = UIColor(named:"toolBarRedColor")
var items = [UIBarButtonItem]()

let selectbutton = UIBarButtonItem(title: "Left Button", style: .plain, target: self, action: #selector(printMe))

let unselectbutton = UIBarButtonItem(title: "Right Button", style: .plain, target: self, action: #selector(printMe))

items.append(selectbutton)
items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil))
items.append(unselectbutton)
self.toolbarItems = items

我想扩大工具栏的高度并尝试执行以下操作:

class CustomToolbar: UIToolbar {

override func sizeThatFits(_ size: CGSize) -> CGSize {

    var newSize: CGSize = super.sizeThatFits(size)
    newSize.height = 80  // there to set your toolbar height

    return newSize
    }

}

什么做错了?高度未调整。

标签: swifttoolbar

解决方案


尝试这个:

let navBarSize = CGSize(width: navigationController!.navigationBar.bounds.width, height: 200)

override func viewDidLayoutSubviews() {
    navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: navBarSize.width, height: navBarSize.height)
}

希望这可以帮助 :)


推荐阅读