【发布时间】:2020-11-08 10:07:18
【问题描述】:
我的 UIToolBar 不想使用整个屏幕宽度。
我不使用任何 TextField,而是将工具栏添加为 UIPickerView 的子视图。
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
@IBAction func addDistanceToCollection(_ sender: UIBarButtonItem) {
createPickerView()
createToolBar()
}
func createPickerView() {
pickerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pickerView)
pickerView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
pickerView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
pickerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
pickerView.addSubview(toolBar)
}
func createToolBar() {
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: nil)
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: nil)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolBar.sizeToFit()
toolBar.setItems([cancelButton, flexibleSpace, doneButton], animated: false)
}
我也试过你使用pickerView.frame.width 作为工具栏宽度,但它似乎不能解决问题。看起来宽度是一些我无法改变的常量值。
【问题讨论】:
-
你能把它设置为 inputview 吗?作为
picker.inputView = toolbar -
或者也设置工具栏的约束?
-
不,我不能将其设置为 inputView
-
你是指哪个约束?
标签: ios swift xcode uiviewcontroller uitoolbar