无法使UIToolBar黑色带白色按钮项目色调(ios 9,Swift)

u84*_*six 1 uitoolbar swift ios9 xcode7

我正在以编程方式将工具栏添加到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
}
Run Code Online (Sandbox Code Playgroud)

小智 7

谢谢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         
Run Code Online (Sandbox Code Playgroud)


u84*_*six 5

我所需要做的就是添加调用 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
}
Run Code Online (Sandbox Code Playgroud)