xcode 有没有办法在 Swift 上隐藏 UIDatePicker?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33223533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:04:48  来源:igfitidea点击:

Is there a way to hide the UIDatePicker on Swift?

xcodeswift

提问by SwaggyMcMuffins

I am trying to hide my datepicker until my textField is selected. I did this simply for a pickerView I have. However, I have not been able to find a solution using Swift to show then hide the datePicker on a textField.

我试图隐藏我的日期选择器,直到我的 textField 被选中。我这样做只是为了我拥有的pickerView。但是,我无法找到使用 Swift 显示然后隐藏 textField 上的 datePicker 的解决方案。

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

@IBOutlet weak var TeeTimes: UILabel!
@IBOutlet weak var textFieldDate: UITextField!
@IBOutlet weak var datePicker: UIDatePicker!
@IBOutlet weak var picker: UIPickerView! = UIPickerView()
@IBOutlet weak var textfieldCourse: UITextField!


var pickerData = ["Norman Course", "Riding Golf", "Dmob Course"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    picker.hidden = true;
    self.textfieldCourse.delegate = self
    self.picker.delegate = self
    self.picker.dataSource = self
    textfieldCourse.text = pickerData[0]
    //Inputs the data into the array
    datePicker.hidden = true;
    datePicker.addTarget(self, action: Selector("datePickerChanged:"), forControlEvents: UIControlEvents.ValueChanged)

}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

}
func numberOfComponentsInPickerView(pickerView: UIPickerView) ->Int {
    return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) ->Int {
    return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
    textfieldCourse.text = pickerData[row]
    picker.hidden = true
}
func textFieldShouldBeginEditing(textfield: UITextField) ->Bool {
    picker.hidden = false
    return false
}
func datePickerChanged(dataPicker: UIDatePicker) {
    datePicker.hidden = false
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle

    let strDate = dateFormatter.stringFromDate(datePicker.date)
    textFieldDate.text = strDate
}

}

}

采纳答案by Dennis G.

I can't see why you wouldn't be able to use the same principle, seeing as both UIDatePickerand UIPickerVieware subclasses of UIView. Simply set the delegate of textFieldDateto selfand apply the same logic within textFieldShouldBeginEditing. Only you will have to check which text field is being edited in order to show the correct picker.

我不明白为什么你不能使用相同的原则,因为UIDatePickerUIPickerView都是UIView 的子类。只需将textFieldDate的委托设置为self并在textFieldShouldBeginEditing 中应用相同的逻辑。只有您必须检查正在编辑哪个文本字段才能显示正确的选择器。

Additionally, you should set datePicker.hiddento truein your datePickerChangedfunction assuming you want the date picker to hide after a date has been selected.

此外,假设您希望在选择日期后隐藏日期选择器,您应该在datePickerChanged函数中将datePicker.hidden设置为true

回答by hacker_1989

We can definitely use auto layout and set constraints to hide the space and collapse different views. You can set auto layout for different objects and set the height of date picker. Drag and drop the height constraint in your controller and provide a name:

我们绝对可以使用自动布局并设置约束来隐藏空间和折叠不同的视图。您可以为不同的对象设置自动布局并设置日期选择器的高度。在控制器中拖放高度约束并提供名称:

enter image description here.

在此处输入图片说明.

It will look something like this:

它看起来像这样:

enter image description here

在此处输入图片说明

Next set the constraint to 0 in your viewDidLoad()like this: datePickerHeight.constant = 0

接下来将约束设置为 0,viewDidLoad()如下所示:datePickerHeight.constant = 0

Now where you are hiding your datePicker set the height to 0 and when you are showing the datePicker set the height to the desired height. You can use:

现在您隐藏 datePicker 的位置将高度设置为 0,当您显示 datePicker 时,将高度设置为所需的高度。您可以使用:

if dateTimePicker.hidden {
    dateTimePicker.hidden = false
    datePickerHeight.constant = 135
} else {
    dateTimePicker.hidden = true
    datePickerHeight.constant = 0
}