在 Swift 的弹出式 datePickerView 中添加完成按钮?

Posted

技术标签:

【中文标题】在 Swift 的弹出式 datePickerView 中添加完成按钮?【英文标题】:Add a Done button within a pop-up datePickerView in Swift? 【发布时间】:2015-03-18 20:46:40 【问题描述】:

我想在 Swift 中弹出的 datePickerView 中添加一个完成按钮。

代码如下:

@IBOutlet var datePicker: UITextField!

@IBAction func dateTextInputPressed(sender: UITextField) 

    var datePickerView = UIDatePicker()
    datePickerView.datePickerMode = UIDatePickerMode.Date

    sender.inputView = datePickerView
    datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)


func handleDatePicker(sender: UIDatePicker) 
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    datePicker.text = dateFormatter.stringFromDate(sender.date)

我可以使用这段代码成功弹出一个datePickerView。

但是在我选择了日期之后,它没有“完成”按钮来关闭它。

那么我怎样才能在其中添加“完成”按钮呢?

【问题讨论】:

在 Swift 5 中 ***.com/questions/27050988/… 【参考方案1】:

我做了这个扩展来关闭选择器。 斯威夫特 2

extension UIToolbar 

 func ToolbarPiker(mySelect : Selector) -> UIToolbar 

    let toolBar = UIToolbar()

    toolBar.barStyle = UIBarStyle.Default
    toolBar.translucent = true
    toolBar.tintColor = UIColor.blackColor()
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: mySelect)
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)

    toolBar.setItems([ spaceButton, doneButton], animated: false)
    toolBar.userInteractionEnabled = true

    return toolBar



斯威夫特 3 - 4

extension UIToolbar 

func ToolbarPiker(mySelect : Selector) -> UIToolbar 

    let toolBar = UIToolbar()

    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.tintColor = UIColor.black
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: mySelect)
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)

    toolBar.setItems([ spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true

    return toolBar



你可以简单地在你的 viewController 中使用它:

  override func viewDidLoad() 
    super.viewDidLoad()

 //SWIFT2
 /*
let toolBar = UIToolbar().ToolbarPiker(#selector(MyViewController.dismissPicker))

 */
// Swift3 - 4
    let toolBar = UIToolbar().ToolbarPiker(mySelect: #selector(MyViewController.dismissPicker))

    myTextField.inputAccessoryView = toolBar




 // Swift 2 - 3
 func dismissPicker()  

    view.endEditing(true)



 // Swift 4
 @objc func dismissPicker()  

    view.endEditing(true)


【讨论】:

优秀的解决方案。关于 swift 3 代码的一个小更正: let toolBar = UIToolbar().ToolbarPiker(mySelect: #selector @JUL2791 在你想要的之前代码 myTextField.inputView = myPikerView 之后,例如在 UITextField (myTextField) 的 Editing Did Begin 函数中 我想在点击按钮时显示选择器,并在从选择器中选择后将其分配给按钮标题。所以 inputAccessoryView 不适用于 UIButton。还有其他解决方案吗?【参考方案2】:

您可以将通用UIView 用于UITextFieldinputView 属性。我们可以将UIDatePickerUIButton 添加到这个UIView

@IBAction func dateTextInputPressed(sender: UITextField) 

    //Create the view
    let inputView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, 240))


    var datePickerView  : UIDatePicker = UIDatePicker(frame: CGRectMake(0, 40, 0, 0))
    datePickerView.datePickerMode = UIDatePickerMode.Date
    inputView.addSubview(datePickerView) // add date picker to UIView

    let doneButton = UIButton(frame: CGRectMake((self.view.frame.size.width/2) - (100/2), 0, 100, 50))
    doneButton.setTitle("Done", forState: UIControlState.Normal)
    doneButton.setTitle("Done", forState: UIControlState.Highlighted)
    doneButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    doneButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)

    inputView.addSubview(doneButton) // add Button to UIView

    doneButton.addTarget(self, action: "doneButton:", forControlEvents: UIControlEvents.TouchUpInside) // set button click event

    sender.inputView = inputView
    datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)

    handleDatePicker(datePickerView) // Set the date on start.


func handleDatePicker(sender: UIDatePicker) 
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    datePicker.text = dateFormatter.stringFromDate(sender.date)


func doneButton(sender:UIButton)

    datePicker.resignFirstResponder() // To resign the inputView on clicking done.

【讨论】:

【参考方案3】:

如果您使用的是按钮而不是文本字段:

/** * MARK - 提前预订操作 */

@IBAction func advanceBookingAction(sender: UIButton) 

    var datePicker : UIDatePicker = UIDatePicker()
    var datePickerContainer = UIView()

    datePickerContainer.frame = CGRectMake(0.0, self.view.frame.height/2, 320.0, 300.0)
    datePickerContainer.backgroundColor = UIColor.whiteColor()

    var pickerSize : CGSize = datePicker.sizeThatFits(CGSizeZero)
    datePicker.frame = CGRectMake(0.0, 20, pickerSize.width, 460)
    datePicker.setDate(NSDate(), animated: true)
    datePicker.maximumDate = NSDate()
    datePicker.datePickerMode = UIDatePickerMode.DateAndTime
    datePicker.addTarget(self, action: "dateChangedInDate:", forControlEvents: UIControlEvents.ValueChanged)
    datePickerContainer.addSubview(datePicker)

    var doneButton = UIButton()
    doneButton.setTitle("Done", forState: UIControlState.Normal)
    doneButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
    doneButton.addTarget(self, action: Selector("dismissPicker:"), forControlEvents: UIControlEvents.TouchUpInside)
    doneButton.frame    = CGRectMake(250.0, 5.0, 70.0, 37.0)

    datePickerContainer.addSubview(doneButton)

    self.view.addSubview(datePickerContainer)

// end advanceBookingAction

/**
* MARK - observer to get the change in date
*/

func dateChangedInDate(sender:UIDatePicker)

    var dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
    objMethodInc.printvj("date selected \(datePicker.date)")

// end dateChangedInDate

/*
* MARK - dismiss the date picker value
*/
func dismissPicker(sender: UIButton) 
    objMethodInc.printvj("dismiss date picker")
    datePickerContainer.removeFromSuperview()
// end dismissPicker

【讨论】:

【参考方案4】:

在 Swift 5 中

如果你只想要 DatePicker 上的 完成 按钮,请使用我的代码

//Write toolbar code for done button
let toolBar = UIToolbar()
toolBar.barStyle = .default
toolBar.isTranslucent = true
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(onClickDoneButton))
toolBar.setItems([space, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
dobTF.inputAccessoryView = toolBar //Change your TextField name here

@objc func onClickDoneButton() 
    self.view.endEditing(true)

【讨论】:

【参考方案5】:
@IBOutlet weak var txtDate: UITextField!
override func viewDidLoad() 
    super.viewDidLoad()
    
    //Write toolbar code for done button
    let toolBar = UIToolbar()
    toolBar.barStyle = .default
    toolBar.isTranslucent = true
    let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(onClickDoneButton))
    toolBar.setItems([space, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true
    toolBar.sizeToFit()
    txtDate.inputAccessoryView = toolBar //Change your TextField name here
    
    let datePickerView = UIDatePicker()
    datePickerView.datePickerMode = UIDatePicker.Mode.date
    
    txtDate.inputView = datePickerView
    datePickerView.addTarget(self, action: #selector(handleDatePicker(sender:)), for: UIControl.Event.valueChanged)
    


@objc func onClickDoneButton() 
    self.view.endEditing(true)


@objc func handleDatePicker(sender: UIDatePicker) 
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    txtDate.text = dateFormatter.string(from: sender.date)

【讨论】:

结合以上 Swift 5 的答案

以上是关于在 Swift 的弹出式 datePickerView 中添加完成按钮?的主要内容,如果未能解决你的问题,请参考以下文章

swift 在swift中创建一个警报视图弹出窗口

swift [Swift]在当前弹出窗口中显示警报(SimpleTransitioningDelegate)

在 Swift 中点击按钮时显示一个弹出框

在屏幕上居中 Swift 弹出窗口

在后台点击 Swift 时禁用关闭弹出框

swift 本地通知弹出,没有徽章