选择日期时如何关闭紧凑的 UIDatePicker 弹出窗口? [英] How to close a compact UIDatePicker popup when choosing a date?

查看:14
本文介绍了选择日期时如何关闭紧凑的 UIDatePicker 弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIDatePicker,它的 Preferred Style 设置为 Compact,我想在用户更改日期后关闭它的日历弹出窗口.我试图寻找答案,但找不到任何人在谈论它.

I have a UIDatePicker whose Preferred Style is set to Compact and I want to close its calendar popup once the user changes the date. I tried to look for an answer but I can't find anyone talking about it.

推荐答案

如果你想实现它,你需要创建一个自定义的 Picker .

If you do want to implement it you need to create a custom Picker .

    UITextField textField;
    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();


        textField = new UITextField(new CGRect(10, 100, 200, 80));

        textField.TextColor = UIColor.Red;
        textField.BackgroundColor = UIColor.LightGray; // you could set the style as you want 
       // textField.Delegate = new MyPickerDelegate();

        UIDatePicker datePicker = new UIDatePicker();
        datePicker.BackgroundColor = UIColor.LightGray;
        datePicker.ValueChanged += DatePicker_ValueChanged;
        
        if(UIDevice.CurrentDevice.CheckSystemVersion(14,0))
        {
            datePicker.PreferredDatePickerStyle = UIDatePickerStyle.Inline;
        }

        else
        {
            datePicker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels;
        }

        textField.InputView = datePicker;

        View.AddSubview(textField);
        // Perform any additional setup after loading the view, typically from a nib.
    }

    private void DatePicker_ValueChanged(object sender, EventArgs e)
    {
        var picker = sender as UIDatePicker;

        var date = picker.Date;

        var dateFormatter =new NSDateFormatter();

        dateFormatter.DateFormat = "YYYY-MM-dd HH:mm";

        var dateString = dateFormatter.ToString(date);
        textField.Text = dateString;
        textField.ResignFirstResponder();


    }


    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        base.TouchesBegan(touches, evt);

        textField.ResignFirstResponder();

    }

这篇关于选择日期时如何关闭紧凑的 UIDatePicker 弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆