在 3.5 和 4 英寸设备上安装 UIPicker 和 3 个文本字段
Fitting UIPicker and 3 text fields on 3.5 and 4 inch devices
我有(从上到下)2 个 UILabel、1 个 UIPickerView、3 个 UITexfield 和 1 个按钮。给定选择器的大小,在键盘出现时它会与我在 3.5 英寸和 4 英寸设备上的文本字段重叠。我使用一些代码在键盘显示时弹出文本字段,但我不确定如何处理选择器。我试着把它隐藏在键盘上,但它太突然了,用户可能想改变他们不能轻易改变的选择。考虑到尺寸限制,我应该如何处理选择器?
override func viewDidLoad() {
super.viewDidLoad()
countyPicker.delegate = self
countyPicker.dataSource = self
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
func keyboardWillShow(sender: NSNotification) {
self.view.frame.origin.y -= 150
}
func keyboardWillHide(sender: NSNotification) {
self.view.frame.origin.y += 150
// countyPicker.hidden = true
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return titleData.mdCounties.count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent componefnt: Int) {
// countyPicker.alpha = 0.3
selectedCounty = titleData.mdCounties[row].name
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return titleData.mdCounties[row].name!
}
您可以用一条简单的线来指示选择器的选择。点击线条时,您可以使用动画显示或隐藏线条下方的选择器。
在 UITableView
中执行此操作可能有意义。当您编辑事件时,看看 Apple 如何在其日历应用程序中显示和隐藏日期选择器。在插入和删除 table 视图行时,您可以使用标准行动画非常漂亮地完成此操作。
table 视图解决方案也更加灵活,因为它始终有效,即使将来必须显示的项目数量会增加,或者如果 Apple 推出新的 iPhone纳米
将1 UIPickerView 和3 UITexfield 放在一个视图中,当键盘出现时弹出整个视图。
因为您已经编写了弹出文本字段的代码,所以它会成功。
希望对您有所帮助。 :)
我有(从上到下)2 个 UILabel、1 个 UIPickerView、3 个 UITexfield 和 1 个按钮。给定选择器的大小,在键盘出现时它会与我在 3.5 英寸和 4 英寸设备上的文本字段重叠。我使用一些代码在键盘显示时弹出文本字段,但我不确定如何处理选择器。我试着把它隐藏在键盘上,但它太突然了,用户可能想改变他们不能轻易改变的选择。考虑到尺寸限制,我应该如何处理选择器?
override func viewDidLoad() {
super.viewDidLoad()
countyPicker.delegate = self
countyPicker.dataSource = self
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
func keyboardWillShow(sender: NSNotification) {
self.view.frame.origin.y -= 150
}
func keyboardWillHide(sender: NSNotification) {
self.view.frame.origin.y += 150
// countyPicker.hidden = true
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return titleData.mdCounties.count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent componefnt: Int) {
// countyPicker.alpha = 0.3
selectedCounty = titleData.mdCounties[row].name
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return titleData.mdCounties[row].name!
}
您可以用一条简单的线来指示选择器的选择。点击线条时,您可以使用动画显示或隐藏线条下方的选择器。
在 UITableView
中执行此操作可能有意义。当您编辑事件时,看看 Apple 如何在其日历应用程序中显示和隐藏日期选择器。在插入和删除 table 视图行时,您可以使用标准行动画非常漂亮地完成此操作。
table 视图解决方案也更加灵活,因为它始终有效,即使将来必须显示的项目数量会增加,或者如果 Apple 推出新的 iPhone纳米
将1 UIPickerView 和3 UITexfield 放在一个视图中,当键盘出现时弹出整个视图。
因为您已经编写了弹出文本字段的代码,所以它会成功。
希望对您有所帮助。 :)