垂直对齐工具栏内的条按钮



我当前正在使用以下代码在用户单击uitextfield上时显示datepicker。这可以正常工作,但是问题在那里,右键在垂直垂直的中心对齐。有什么方法可以做到这一点吗?我尝试在按钮上使用UIOFFSET,但没有结果。

private func setupDatePicker() {
    let toolBar = UIToolbar(frame: CGRect(x: 0, y: self.view.frame.size.height/6,
                                          width:  self.view.frame.size.width, height: 40.0))
    toolBar.layer.position = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height-20.0)
    toolBar.barStyle = UIBarStyle.blackTranslucent
    toolBar.tintColor = UIColor.white
    toolBar.backgroundColor = UIColor.black
    let cancelBtn = UIBarButtonItem(title: "cancel".localized(lang: Localize.currentLanguage()), style: UIBarButtonItemStyle.plain, target: self,
                                    action: #selector(tapppedToolBarBtn))

    let okBarBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self,
                                   action: #selector(donePressed))
    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace,
                                    target: self, action: nil)
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 3,
                                      height: self.view.frame.size.height))
    label.font = UIFont(name: "Roboto-Regular", size: 12)
    label.backgroundColor = UIColor.clear
    label.textColor = UIColor.white
    label.text = "selectdate".localized(lang: Localize.currentLanguage())
    label.textAlignment = NSTextAlignment.center
    let textBtn = UIBarButtonItem(customView: label)
    toolBar.setItems([cancelBtn, flexSpace, textBtn, flexSpace, okBarBtn], animated: true)
    startDateTxf.inputAccessoryView = toolBar
}

结果:DatePicker工具栏的结果

我通过使用以下代码设法解决了此对齐问题:

let toolBar = UIToolbar(frame: CGRect(x: 0, y: self.view.frame.size.height/6,
                                          width:  self.view.frame.size.width, height: 40.0))
    toolBar.layer.position = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height-20.0)
    toolBar.barStyle = UIBarStyle.blackTranslucent
    toolBar.tintColor = UIColor.white
    toolBar.backgroundColor = UIColor.black
    let cancelBtn1 = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 4,
                                            height: self.view.frame.size.height))
    cancelBtn1.setTitle("cancel".localized(lang: Localize.currentLanguage()), for: .normal)
    cancelBtn1.addTarget(self, action: #selector(tapppedToolBarBtn), for: .touchUpInside)
    let cancelBtn = UIBarButtonItem(customView: cancelBtn1)
    let okBarBtn1 = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 4,
                                            height: self.view.frame.size.height))
    okBarBtn1.setTitle("Done", for: .normal)
    okBarBtn1.addTarget(self, action: #selector(donePressed(sender:)), for: .touchUpInside)
    let okBarBtn = UIBarButtonItem(customView: okBarBtn1)
    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace,
                                    target: self, action: nil)
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 3,
                                      height: self.view.frame.size.height))
    label.font = UIFont(name: "Roboto-Regular", size: 12)
    label.backgroundColor = UIColor.clear
    label.textColor = UIColor.white
    label.text = "selectdate".localized(lang: Localize.currentLanguage())
    label.textAlignment = NSTextAlignment.center
    let textBtn = UIBarButtonItem(customView: label)
    toolBar.setItems([cancelBtn, flexSpace, textBtn, flexSpace, okBarBtn], animated: true)
    startDateTxf.inputAccessoryView = toolBar

结果是:结果模拟器

最新更新