如何在Swift中自定义工具栏项目?

时间:2018-07-23 07:54:27

标签: swift uibutton uibarbuttonitem uitoolbar uitoolbaritem

我目前正在开发一个项目,该项目需要像这样的底部的工具栏:

my toolbar

我做了这样的事情:

@IBOutlet weak var doneBtn: UIButton!
@IBOutlet weak var bookmarkBtn: UIButton!
@IBOutlet weak var paddingLeft: UIBarButtonItem!
@IBOutlet weak var paddingRight: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    if UIDevice.current.model.hasPrefix("iPad") {
        paddingLeft.width = -12
        //paddingRight.width = -12
    } else {
        paddingLeft.width = -20
        //paddingRight.width = -20
    }
}

override func viewDidLayoutSubviews() {
    setupToolbar()
    print(self.view.bounds.size)
}

func setupToolbar() {
    self.navigationController?.toolbar.frame.size.width = self.view.bounds.size.width
    let doneBtnHeight = self.navigationController?.toolbar.bounds.size.height       
    let doneBtnWidth = (self.navigationController?.toolbar.frame.size.width)! - bookmarkBtn.frame.size.width

    doneBtn.frame = CGRect(x: 0, y: 0, width: doneBtnWidth, height: doneBtnHeight!)
}

enter image description here

您会看到右侧有很多间距,并且按钮的宽度不正确。 (在iPad上效果很好,但在iPhone上效果不佳)有人可以启发我吗?

0 个答案:

没有答案