PickerView无法改变第二行的颜色

时间:2018-04-03 05:44:20

标签: ios swift uipickerview

我在文本字段输入视图中使用选择器视图。它工作得很完美,但是当我选择第二行然后它将颜色改变为红色然后我关闭选择器并且当我打开第二次选择器时所选行的颜色没有改变但是它不能仅用于第二行。其他行完美地工作

这是MyCode

{{1}}

输出:

首次选择第二行时

enter image description here

然后关闭选择器并再次打开选择器enter image description here 第二行不变红色

1 个答案:

答案 0 :(得分:0)

使用didSelectRow的{​​{1}}方法并更改字体颜色和字体大小

<强>更新

我已将此代码添加到选择中,以避免代码重复

UIPickerViewDelegate

修复预选问题

您只需在func applySelection(pickerView : UIPickerView, row:Int, component:Int) { if let selectedView = pickerView.view(forRow: row, forComponent: component) as? UILabel { selectedView.attributedText = NSAttributedString(string: (selectedView.attributedText?.string)!, attributes: [NSAttributedStringKey.font:GlobalFont.Font_GillSansStd(fontsize: 26), NSAttributedStringKey.foregroundColor: GlobalColor.RED_THEME]) } } 或显示pickerView

时添加此代码即可
viewDidAppear

然后您可以像这样使用

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.applySelection(pickerView: self.pickerView, row: 0, component: 0)
}