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

标签 swift xcode swift3

我目前正在使用以下代码在用户单击 UITextfield 时显示日期选择器。这工作正常,但问题是左右按钮没有正确垂直居中对齐。有什么办法可以做到这一点?我尝试在按钮上使用 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
}

结果: Result of the datepicker toolbar

最佳答案

我设法使用以下代码解决了这个对齐问题:

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

结果是:result in simulator

关于swift - 垂直对齐工具栏内的条形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43611827/

相关文章:

ios - 尽管有限制,xcode 按钮仍然重叠

c++ - max和fmax的区别(跨平台编译)

swift - 你如何在 Swift 3 中编写完成处理程序?

ios - 我们如何向 UINavigation 添加额外的 UILabel?

Swift3,如何在字典中添加 AnyObject?

ios - 在 UITableViewController (Swift) 顶部添加按钮

Swift 从 'UIViewController?' 转换为不相关类型 'TableViewCell' 总是失败

iphone - 修复 iPad 状态栏方向为纵向

ios - 自定义组件背景透明

ios - "openURLContexts"函数在ios13中没有被调用