使用 UIDatePickerMode.DateAndTime 时 UIDatePicker 中断

发布于 2024-12-19 03:29:10 字数 2977 浏览 1 评论 0原文

我正在使用 Monotouch 5 创建一个应用程序,并且在 iPad 模拟器中运行我的应用程序时,在 iOS 4.3 或更低版本上创建模式为 UIDatePickerMode.DateAndTime 的日期选择器时遇到问题。尽管我为其框架和周围视图设置的尺寸非常好,但 DatePicker 总是损坏。

这是在装有 iOS 4.3 的 iPad 模拟器上的样子:

http://screencast.com/t/isIRcUcKCF5< /a>

这是它在 iOS 5.0 的 iPad 模拟器上的样子:

http://screencast.com/t/UxPTpR0Btzb

在 iPhone 模拟器或真实设备上不会出现此问题。我知道在装有 iOS 5 的真实 iPad 设备上也不会发生这种情况,但我无法测试 4.3 或更低版本,因为我没有这样的设备。

创建日期选择器的源代码如下:

public override void Selected (MonoTouch.Dialog.DialogViewController dvc, UITableView tableView, NSIndexPath path)
        {
            var vc = new MyViewController (this) {
                Autorotate = dvc.Autorotate
            };
            datePicker = CreatePicker ();
            datePicker.Frame = PickerFrameWithSize (datePicker.SizeThatFits (SizeF.Empty));
            //vc.View encompasses the entire, screen so there is plenty of space available for the date picker                           
            vc.View.BackgroundColor = UIColor.Black;
            vc.View.AddSubview (datePicker);
            dvc.ActivateController (vc);
        }


public virtual UIDatePicker CreatePicker ()
    {
        var picker = new UIDatePicker (RectangleF.Empty){
            AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
            Mode = UIDatePickerMode.DateAndTime,
            Date = DateValue,
        };

        return picker;
    }

    static RectangleF PickerFrameWithSize (SizeF size)
    {                                                                                                                                   
        var screenRect = UIScreen.MainScreen.ApplicationFrame;
        float fY = 0, fX = 0;

        switch (UIApplication.SharedApplication.StatusBarOrientation){
        case UIInterfaceOrientation.LandscapeLeft:
        case UIInterfaceOrientation.LandscapeRight:
            fX = (screenRect.Height - size.Width) /2;
            fY = (screenRect.Width - size.Height) / 2 -17;
            break;

        case UIInterfaceOrientation.Portrait:
        case UIInterfaceOrientation.PortraitUpsideDown:
            fX = (screenRect.Width - size.Width) / 2;
            fY = (screenRect.Height - size.Height) / 2 - 25;
            break;
        }
        // calculated values:
        // fx: 224
        // fy: 369
        // size.Width: 320
        // size.Height: 320
        return new RectangleF (fX, fY, size.Width, size.Height);
    }  

当我使用模式为 UIDatePickerMode.Date 或 UIDatePickerMode.Time 的日期选择器时,不会出现该问题。

该问题看起来就像 iPad 上的空白 UIDatePicker 模式 UIDatePickerModeDateAndTime,但是由于某些奇怪的原因,那里描述的修复在我的情况下不起作用。

任何有关如何解决此错误的提示将不胜感激。

谢谢,

阿德里安

I'm creating an app with Monotouch 5 and I'm having a problem with creating a datepicker with the mode UIDatePickerMode.DateAndTime on iOS 4.3 or lower when running my app in an iPad Simulator. The DatePicker is always broken, although the dimensions I set for it's frame and the surrounding view are perfectly fine.

This is how it looks like on an iPad Simulator with iOS 4.3:

http://screencast.com/t/isIRcUcKCF5

and this is how it looks on an iPad Simulator with iOS 5.0:

http://screencast.com/t/UxPTpR0Btzb

This problem does not occur on iPhone Simulators or real devices. I know it also does not occur on real iPad devices with iOS 5, but I could not test 4.3 or lower because I have no such device.

The source code for creating the date picker is as follows:

public override void Selected (MonoTouch.Dialog.DialogViewController dvc, UITableView tableView, NSIndexPath path)
        {
            var vc = new MyViewController (this) {
                Autorotate = dvc.Autorotate
            };
            datePicker = CreatePicker ();
            datePicker.Frame = PickerFrameWithSize (datePicker.SizeThatFits (SizeF.Empty));
            //vc.View encompasses the entire, screen so there is plenty of space available for the date picker                           
            vc.View.BackgroundColor = UIColor.Black;
            vc.View.AddSubview (datePicker);
            dvc.ActivateController (vc);
        }


public virtual UIDatePicker CreatePicker ()
    {
        var picker = new UIDatePicker (RectangleF.Empty){
            AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
            Mode = UIDatePickerMode.DateAndTime,
            Date = DateValue,
        };

        return picker;
    }

    static RectangleF PickerFrameWithSize (SizeF size)
    {                                                                                                                                   
        var screenRect = UIScreen.MainScreen.ApplicationFrame;
        float fY = 0, fX = 0;

        switch (UIApplication.SharedApplication.StatusBarOrientation){
        case UIInterfaceOrientation.LandscapeLeft:
        case UIInterfaceOrientation.LandscapeRight:
            fX = (screenRect.Height - size.Width) /2;
            fY = (screenRect.Width - size.Height) / 2 -17;
            break;

        case UIInterfaceOrientation.Portrait:
        case UIInterfaceOrientation.PortraitUpsideDown:
            fX = (screenRect.Width - size.Width) / 2;
            fY = (screenRect.Height - size.Height) / 2 - 25;
            break;
        }
        // calculated values:
        // fx: 224
        // fy: 369
        // size.Width: 320
        // size.Height: 320
        return new RectangleF (fX, fY, size.Width, size.Height);
    }  

When I am using a datepicker with the modes UIDatePickerMode.Date or UIDatePickerMode.Time, the problem does not occur.

The problem looks just like blank UIDatePicker on iPad in mode UIDatePickerModeDateAndTime, but the fix described there does not work in my case for some weird reason.

Any hints on how to work around this bug would be greatly appreciated.

Thanks,

Adrian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身泪倾城 2024-12-26 03:29:10

我仍然不知道如何使上面的代码工作,但我设法通过使用 UIPopupView 来显示日历来解决这个 iOS 错误。

I still don't have a clue how to make the code above work, but I managed to work around this iOS bug by using a UIPopupView for displaying the calendar instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文