下面是「小時/分/秒」覆蓋另一種解決方案。它使用拾取器視圖委託中的pickerView(UIPickerView, didSelectRow: Int, inComponent: Int)
方法來更新視圖的標籤。
這樣,無論屏幕方向如何,它都能正常工作。 它仍然不是最佳的但它非常簡單。
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = UILabel()
label.text = String(row)
label.textAlignment = .center
return label
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if let label = pickerView.view(forRow: row, forComponent: component) as? UILabel {
if component == 0, row > 1 {
label.text = String(row) + " hours"
}
else if component == 0 {
label.text = String(row) + " hour"
}
else if component == 1 {
label.text = String(row) + " min"
}
else if component == 2 {
label.text = String(row) + " sec"
}
}
}
已經是顯示選擇器視圖時,行必須以編程方式選擇的重疊出現...
func selectPickerViewRows() {
pickerView.selectRow(0, inComponent: 0, animated: false)
pickerView.selectRow(0, inComponent: 1, animated: false)
pickerView.selectRow(30, inComponent: 2, animated: false)
pickerView(pickerView, didSelectRow: 0, inComponent: 0)
pickerView(pickerView, didSelectRow: 0, inComponent: 1)
pickerView(pickerView, didSelectRow: 30, inComponent: 2)
}
來源
2017-06-19 14:41:32
nyg
使用帶有3個組件的uiPickerView,每個組件都有數組。 HourArray從(1到12)。 MinutesArray(1到60)。秒數組(1到60) –
@safecase。我從技術上理解它是如何完成的。我所要求的是,如果有人想與我分享這段代碼,而不是我從零開始寫它 –
@EmilioPelaez - 請不要使用「我們」。你不會在這裏爲整個iOS開發社區說話。有很多優秀的開發人員可以愉快地分享知識和代碼。如果我最終編寫代碼,我會在這裏與其他社區和任何未來可能感興趣的人分享,並閱讀我的文章 –