【问题标题】:Can't make UIToolBar black color with white button item tint (ios 9, Swift)无法使用白色按钮项色调使 UIToolBar 变为黑色(ios 9,Swift)
【发布时间】:2016-01-19 17:46:32
【问题描述】:

我正在以编程方式将工具栏添加到 UIPickerView,这样我就可以拥有一个“完成”按钮,并且我想将 UIToolBar 设置为黑色,将栏项设置为白色。该文档说,如果您想要一个不透明的 UIToolBar,则必须将其半透明设置为 false 并将 barStyle 设置为黑色。我已经这样做了,UIToolBar 仍然是白色的。

private func pickerViewSetup() {

    let pickerView = UIPickerView()
    pickerView.delegate = self
    pickerView.dataSource = self
    pickerView.backgroundColor = .whiteColor()
    pickerView.showsSelectionIndicator = true

    let toolBar = UIToolbar()
    toolBar.translucent = false
    toolBar.barStyle = .Black

    let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "donePicker")
    doneButton.tintColor = UIColor.whiteColor()

    let flexibleSpaceItem = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: "Flexible Space")

    toolBar.setItems([flexibleSpaceItem, doneButton], animated: false)
    toolBar.userInteractionEnabled = true

    pickerTextField.inputView = pickerView
    pickerTextField.inputAccessoryView = toolBar
}

【问题讨论】:

    标签: swift ios9 xcode7 uitoolbar


    【解决方案1】:

    感谢 u84six 的回答。你可以这样做:

    toolBar.tintColor = UIColor.whiteColor()//"Done" button colour
    toolBar.barTintColor = UIColor.blackColor()// bar background colour 
    toolBar.sizeToFit()// Very important, the barTintColor will not work without this         
    

    【讨论】:

      【解决方案2】:

      我需要做的就是添加调用 toolBar.sizeToFit() 并修复所有颜色问题。这是完整的工作代码:

      private func pickerViewSetup() {
      
          let pickerView = UIPickerView()
          pickerView.delegate = self
          pickerView.dataSource = self
          pickerView.backgroundColor = .whiteColor()
          pickerView.showsSelectionIndicator = true
      
          let toolBar = UIToolbar()
          toolBar.barStyle = UIBarStyle.Black
          toolBar.tintColor = UIColor.whiteColor()
          toolBar.sizeToFit()
      
          let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "donePicker")
          let flexibleSpaceItem = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: "Flexible Space")
      
          toolBar.setItems([flexibleSpaceItem, doneButton], animated: false)
          toolBar.userInteractionEnabled = true
      
          pickerTextField.inputView = pickerView
          pickerTextField.inputAccessoryView = toolBar
      }
      

      【讨论】:

      • call sizeToFit() 就是这么含蓄!!
      • 仅供未来的开发者使用,如果您的UIBarButtonItem 不可点击,那么这就是解决方案。 sizeToFit() 也解决了我遇到的这个问题。
      【解决方案3】:

      这还不够。添加

      toolBar.backgroundColor = UIColor.BlackColor();
      

      【讨论】:

      • 这并没有改变任何东西。还是白的。
      【解决方案4】:

      toolBar 不是所需的颜色,因为 toolBar.backgroundColor 设置不正确。使用

      将其设置为BlackColor
      toolBar.backgroundColor = UIColor.BlackColor()
      

      【讨论】:

      • @SergueiFedorov 这正是解决用户问题的方法,所以它是唯一的答案,它就是答案
      • 简单地设置背景颜色对我不起作用。看我的回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2010-10-30
      • 1970-01-01
      相关资源
      最近更新 更多