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