将UIBarButtonItem在工具栏中垂直居中

时间:2016-05-09 19:44:03

标签: ios swift2 uikit

我有一个有两个按钮的uitoolbar,第一个是系统按钮项目Camera,第二个是系统项目可混合空间,第三个是带文本的普通按钮。但是,当工具栏出现时,第一个按钮居中但普通按钮不是。

enter image description here

有关如何将右键更改为垂直居中的任何想法? 这是设置工具栏的代码:

    let sendButton = UIBarButtonItem(title: "test", style: .Plain, target: self, action: #selector(MyClass.test(_:)))
    let toolBarItems = [UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: nil),
                        UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil),
                        sendButton]

1 个答案:

答案 0 :(得分:0)

这是我试过的代码。

import UIKit




class ViewController2 : UIViewController {
@IBOutlet weak var mytoolbar: UIToolbar!
override func viewDidLoad() {

    super.viewDidLoad();

    let sendButton = UIBarButtonItem(title: "test", style: .Plain, target: self, action: nil)
    let toolBarItems = [UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: nil),
                        UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil),
                        sendButton]
    mytoolbar.items = toolBarItems
}




}

编辑:我正在使用以下代码以编程方式创建uitoolbar。

 let mytoolbar = UIToolbar.init(frame: CGRect(x: 0  , y: 0, width:self.view.frame.size.width, height: 44))
        mytoolbar.backgroundColor = UIColor.blueColor()
        let sendButton = UIBarButtonItem(title: "test", style: .Plain, target: self, action: nil)
        let toolBarItems = [UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: nil),
                            UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil),
                            sendButton]
       mytoolbar.items = toolBarItems

    self.view.addSubview(mytoolbar)

希望现在有所帮助 结果如下:enter image description here 它是我的中心。你能告诉我你添加UIToolbar的代码吗?