0

如何在 Swift 3 中创建 UIToolbar?像大多数应用程序一样,我想要左侧的动作和右侧的动作以及中间的标题。它也看起来像一个没有返回操作箭头或堆栈的导航控制器。

代码是这样的:

let buttona = UIBarButtonItem(title: "Buttona", style: .plain, target: self, action: #selector(buttonaToolbar(sender:)));
let buttonb = UIBarButtonItem(title: "Buttonb", style: .plain, target: self, action: #selector(buttonbToolbar(sender:)));

let title = UILabel();

toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 44));
4

1 回答 1

5

这是使用 swift 3 制作带有两个按钮和标题的工具栏的方法:

func createToolbar(){
// ToolBar
    let toolBar = UIToolbar()
    toolBar.barStyle = .blackTranslucent
    toolBar.isTranslucent = true 
    toolBar.backgroundColor = UIColor.blue
    toolBar.tintColor = UIColor.white
    toolBar.sizeToFit()
    // Adding Button ToolBar
    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(EnqiryViewController.doneClick))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(YourViewContreoller.cancelClick))
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true
}

现在为动作编写函数:

func doneClick() {
   //do something here
}

func cancelClick() {
   //do something here
}
于 2017-09-06T04:41:47.233 回答