iOS工具栏barbuttonitem分隔符

时间:2018-11-23 03:45:05

标签: ios iphone swift uibarbuttonitem uitoolbar

我试图在iOS的工具栏中向左右两侧分别添加两个工具栏按钮[取消]和[保存]。

我使用了第三个条形按钮[Spacer]并将其设置为[.flexiblewidth],否则,当添加它时,仅会出现左侧按钮[Cancel],然后必须消失的[Spacer]&和[Save] ?

该屏幕截图位于链接中: https://ibb.co/cZsaVV

let pickerView = UIPickerView()

override func viewDidLoad() {


pickerView.addSubview(self.setToolBar())
}

func setToolBar() -> UIToolbar {
      let toolBar =  UIToolbar()
        toolBar.isTranslucent  = true
        toolBar.backgroundColor = UIColor.clear
        let barButtonAttr = [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15),
                             NSAttributedString.Key.foregroundColor : UIColor.black]
        // [Save] BarButtonItem
        let saveBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.save, target: self, action: nil)
        saveBarButtonItem.setTitleTextAttributes(barButtonAttr, for: .normal)
        // [Cancel] BarButtonItem
        let cancelBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.cancel, target: self, action: nil)
        cancelBarButtonItem.setTitleTextAttributes(barButtonAttr, for: .normal)

        let spacerBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace ,
                                                  target: self, action: nil)
        spacerBarButtonItem.setTitleTextAttributes(barButtonAttr, for: .normal)
        // add BarButtonItems to toolBar
        toolBar.items = [cancelBarButtonItem,spacerBarButtonItem,saveBarButtonItem]
        toolBar.sizeToFit()
return toolBar
}

2 个答案:

答案 0 :(得分:0)

func createAccessoryViewWithTarget(_ target: AnyObject, width: CGFloat) -> UIView {

    // Previous button
    let previousButton = UIBarButtonItem(title: "Previous", style: .plain, target: target, action: #selector(self.moveToPreviousTextField))
    previousButton.tintColor = UIColor.white

    //Next button
    let nextButton = UIBarButtonItem(title: "Next", style: .plain, target: target, action: #selector(self.moveToNextTextField))
    nextButton.tintColor = UIColor.white

    // Dismiss/close/done button
    let doneButton = UIBarButtonItem(title: "Done", style: .done, target: target, action: #selector(self.keyboardDoneButtonTapped))
    doneButton.tintColor = UIColor.white

    let keyboardToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: width, height: 44))
    keyboardToolbar.barStyle = .black

    let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)

    var itemsArray = [UIBarButtonItem]()
    itemsArray.append(previousButton)
    itemsArray.append(fixedSpace)
    itemsArray.append(nextButton)
    itemsArray.append(flexSpace)
    itemsArray.append(doneButton)
    keyboardToolbar.items = itemsArray
    keyboardToolbar.sizeToFit()

    return keyboardToolbar
}

我想这可能是swift3的旧代码。我在这里添加3个按钮,上一个,下一个和完成按钮。按钮之间的空间使用灵活的空间和固定的空间。这里要注意的重要事项是添加barbutton项的顺序。在您的情况下,请使用灵活的空间将两个按钮分别按左端按钮,灵活空间和右端按钮的顺序放在左右两端。

答案 1 :(得分:0)

我终于有了解决方案。

我将ToolBar添加到PickerView,然后调用在所有情况下都必须的UIToolBar.SizeToFit()。

问题是我必须稍后更改选择器视图的大小,在这种情况下,子视图ToolBar的大小无法适应pickerView的新大小协调。因此,该解决方案只需在修改父视图后再次调用ToolBar.SizeToFit()。这是代码的片段:

// popupView是自定义的UIPickerView

popupView.frame = CGRect(x:0,y:0,宽度:100,高度:100)

// toolBar是自定义UIPickerView AddCurrencyPicker的UIToolBar的对象

(popupView as!AddCurrencyPicker).toolBar.sizeToFit()