Can';t使UIToolBar为黑色,带有白色按钮项目色调(ios 9,Swift)
我正在以编程方式向UIPickerView添加一个工具栏,这样我就可以有一个“完成”按钮,并且我希望将UIToolBar设置为黑色,将工具栏项设置为白色。医生说如果你想要一个不透明的UIToolBar,你必须将它的半透明设置为false,并将barStyle设置为黑色。我这样做了,UIToolBar仍然是白色的Can';t使UIToolBar为黑色,带有白色按钮项目色调(ios 9,Swift),swift,ios9,xcode7,uitoolbar,Swift,Ios9,Xcode7,Uitoolbar,我正在以编程方式向UIPickerView添加一个工具栏,这样我就可以有一个“完成”按钮,并且我希望将UIToolBar设置为黑色,将工具栏项设置为白色。医生说如果你想要一个不透明的UIToolBar,你必须将它的半透明设置为false,并将barStyle设置为黑色。我这样做了,UIToolBar仍然是白色的 private func pickerViewSetup() { let pickerView = UIPickerView() pickerView.delegate
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
}
工具栏
不是所需的颜色,因为未正确设置工具栏.backgroundColor
。将其设置为BlackColor
with
toolBar.backgroundColor = UIColor.BlackColor()
这还不够。加
toolBar.backgroundColor = UIColor.BlackColor();
我所需要做的就是添加call 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
}
谢谢你的回答。您可以这样做:
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
这没有改变任何事情。仍然是白色。@SergueiFedorov这正是解决用户问题的方法,因此这是唯一的答案,也是唯一的答案。只是设置背景色对我不起作用。请看我的答案。call
sizeToFit()
太含蓄了!!对于未来的开发者来说,如果你的UIBarButtonItem
不可点击,那么这就是解决方案sizeToFit()
还修复了我的这个问题。