据说是iOS10下的问题,我的是iOS12.1
尝试了各种办法,包括
let row = picker.selectedRow(inComponent: 0)
picker.selectRow(row, inComponent: 0, animated: false)
等方法,但是还是没用啊!
最后用了比较笨的方法。你不是第一次不显示吗?那我就先预热一次!
class DMPicker: UIView {
...
private var picker: UIPickerView!
func show() {
UIApplication.shared.keyWindow?.addSubview(self)
picker.reloadAllComponents()
}
//第一次展示不能显示出选中线,所以预热一次,先把选中线展示出来
func testShow() {
self.isHidden = true
UIApplication.shared.keyWindow?.addSubview(self)
picker.reloadAllComponents()
self.removeFromSuperview()
self.isHidden = false
}
}
ViewController中,
lazy var picker: DMPicker = {
let apicker = DMPicker(frame: CGRect.zero)
apicker.keyofValue = "teamName"
apicker.title = "选择班组"
apicker.didSelectedRow = {
(dic) in
self.selectedTeam = dic
}
return apicker
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
picker.testShow() //预热
}
//实际显示的时候
fun someFunc() {
self.picker.itemArray = self.teams
self.picker.show()
}
这样就能在‘第一次’显示数据的时候,让选中线显示出来
网友评论