【问题标题】:Add datepicker in UIActionsheet using Swift使用 Swift 在 UIActionsheet 中添加日期选择器
【发布时间】:2014-09-11 06:31:54
【问题描述】:

有什么方法可以显示 UIAlertControllerStyle.ActionSheet 中的日期选择器吗?有办法在 ActionSheet 中添加按钮并向这些按钮添加操作,但我找不到在 ActionSheet 中添加日期选择器或任何自定义视图的解决方案。

【问题讨论】:

    标签: swift datepicker uiactionsheet


    【解决方案1】:

    这是不可能的,但您可以使用 UItextfield 输入视图和附件视图在用户单击文本字段时显示和关闭日期选择器

    class KPDatePickerViewController: UIViewController {
        var datePicker:UIDatePicker!
        @IBOutlet var dateTextField:UITextField!
    
        override func viewDidLoad() {
            var customView:UIView = UIView (frame: CGRectMake(0, 100, 320, 160))
            customView.backgroundColor = UIColor.brownColor()
            datePicker = UIDatePicker(frame: CGRectMake(0, 0, 320, 160))
            customView .addSubview(datePicker)
            dateTextField.inputView = customView
            var doneButton:UIButton = UIButton (frame: CGRectMake(100, 100, 100, 44))
            doneButton.setTitle("Done", forState: UIControlState.Normal)
            doneButton.addTarget(self, action: "datePickerSelected", forControlEvents: UIControlEvents.TouchUpInside)
            doneButton.backgroundColor = UIColor .blueColor()
            dateTextField.inputAccessoryView = doneButton
        }
    
        func datePickerSelected() {
            dateTextField.text =  datePicker.date.description
        }
    }
    

    【讨论】:

    • 谢谢 karthik 我尝试了同样的事情,但不知道如何消除这个观点!
    • 我使用 resignFirstResponder() 将其关闭,效果很好:)
    【解决方案2】:

    是的,绝对可以在 UIAlertController 中显示 UIDatePicker。在 Swift 4 中查看以下代码。我在 Projects 中多次使用此代码,并且运行良好。

    let dateChooserAlert = UIAlertController(title: "Choose date...", message: nil, preferredStyle: .actionSheet)
    dateChooserAlert.view.addSubview(datePicker)
    dateChooserAlert.addAction(UIAlertAction(title: "Done", style: .cancel, handler: { action in
            // Your actions here if "Done" clicked...
        }))
    let height: NSLayoutConstraint = NSLayoutConstraint(item: dateChooserAlert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.1, constant: 300)
    dateChooserAlert.view.addConstraint(height)
    self.present(dateChooserAlert, animated: true, completion: nil)
    

    【讨论】:

    • 这可行,但有一点需要注意。因为您为UIAlertAction 指定了style: .cancel,所以当用户在警报范围之外点击以将其关闭时,将触发相同的处理程序。您可以使用style: .default,但随后您将无法在外面点击以关闭。我最终有两个动作,一个是style: .default 的“完成”,一个是style: .cancel 的“取消”。生成的工作表 UI 不太好,但这是我唯一的选择。
    • 可以添加以将尺寸限制在警报表datePicker.translatesAutoresizingMaskIntoConstraints = false datePicker.topAnchor.constraint(equalTo: dateChooserAlert.view.topAnchor, constant: 15).isActive = true datePicker.bottomAnchor.constraint(equalTo: dateChooserAlert.view.bottomAnchor, constant: -15).isActive = true datePicker.leadingAnchor.constraint(equalTo: dateChooserAlert.view.leadingAnchor, constant: 0).isActive = true datePicker.trailingAnchor.constraint(equalTo: dateChooserAlert.view.trailingAnchor, constant: 0).isActive = true
    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多