【问题标题】:UIToolBar does not use the whole width of the screenUIToolBar 不使用屏幕的整个宽度
【发布时间】: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


【解决方案1】:

也为工具栏设置约束

func createPickerView() {
    pickerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(pickerView)
    view.addSubview(toolBar)
    
    pickerView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    pickerView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    pickerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    
    toolBar.translatesAutoresizingMaskIntoConstraints = false
    toolBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    toolBar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    toolBar.bottomAnchor.constraint(equalTo: pickerView.topAnchor).isActive = true
    toolBar.heightAnchor.constraint(equalToConstant: 50).isActive = true
}

【讨论】:

  • 谢谢。我尝试了这段代码,但应用程序因以下堆栈跟踪而崩溃:*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutXAxisAnchor:0x280c85100 "UIPickerView:0x137f098d0.leading"> and <NSLayoutXAxisAnchor:0x280c85680 "UILayoutGuide:0x2820c80e0'UIViewSafeAreaLayoutGuide'.leading"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.' terminating with uncaught exception of type NSException
  • 你添加了相同的代码吗?将工具栏添加到pickerView后添加约束...
  • hm,应用程序不再崩溃,但不幸的是 UIToolBar 不可见:(
  • 可以设置成pickerView.inputAccessoryView = toolbar吗?
  • 很遗憾没有,错误提示Cannot find 'toolbar' in scope
猜你喜欢
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
相关资源
最近更新 更多