【问题标题】:UIDatePicker stopped working Xamarin IOS14UIDatePicker 停止工作 Xamarin IOS14
【发布时间】:2021-01-15 22:37:41
【问题描述】:

由于我已在我的 Mac 上更新到 Xcode 12 和 VS(2019 8.7.8 build 4)并将 Xamarin 更新到最新版本(Xamarin.iOS 14.0.0.0),我发现我无法使用 UIDatePicker。

DatePicker

查看文档,https://developer.apple.com/documentation/uikit/uidatepicker 我需要设置 Style,也许还需要设置 preferredDatePickerStyle,但这些都不是我可以在 (Xamarin) 代码中设置的属性。

有没有人找到解决这个问题的方法来选择日期?

【问题讨论】:

  • 添加所用程序的所有版本是个好主意。 “最新”版本无法映射到您正在使用的实际版本。并且由于这可能是一个使用的应用程序的某些不正确(或尚未更新)版本的问题,因此实际版本对于其他人查看您的问题至关重要。
  • 已更新以包含特定版本

标签: c# xamarin uidatepicker ios14


【解决方案1】:

一旦我们设置了preferredDatePickerStylesizeToFit,它就会起作用,示例代码如下。

_datePicker = new UIDatePicker(new CGRect(0, 30, 0, 0));
_datePicker.AutoresizingMask = UIViewAutoresizing.FlexibleRightMargin;
_datePicker.Frame = new CGRect(_datePicker.Frame.Location, new CGSize(300, _datePicker.Frame.Size.Height));
_datePicker.Mode = _datePickerMode;
_datePicker.Date = (NSDate)_defaultDate;
_datePicker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels; //Add this in ios14
_datePicker.SizeToFit(); //Add this in ios14

【讨论】:

  • 会的,但是两个 ios14 选项在我使用的版本中不可用。我相信他们会的!
  • 如何在自定义渲染器中利用这一点? iOS 渲染器中 DatePicker 的 Control 对象是 UITextField 而不是 UIDatePicker
  • @SomeStudent 无论您在控件对象中分配 DatePicker 的哪个对象,您都应该可以设置上述内容,如果没有尝试<UIDatePickerObject>.SetValueForKey(new NSNumber(1), new NSString("preferredDatePickerStyle"))
【解决方案2】:

我已经解决了这个问题:

_datePicker.SetValueForKey(new NSNumber(1), new NSString("preferredDatePickerStyle"));

现在显示正确的选择器

Date Selector

【讨论】:

  • 正如您在下面回答的那样,我没有使用_datePicker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels;,而是使用键和枚举数设置值(1 表示轮子)。
  • 但只设置PreferredDatePickerStyle 对我不起作用,它只有在我添加SizeToFit 后才开始工作。
【解决方案3】:

我基本上将其他答案的所有细节都放在了public class CustomDatePickerRenderer : DatePickerRenderer 中针对 iOS 中 Xamarin 表单的相应实现中(为简洁起见,我省略了 CreateNativeControlOnElementPropertyChanged 和自定义方法的实现):

protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
    const int iosVersionMajorForDatePickerStyleFeature = 13;
    const int iosVersionMinorForDatePickerStyleFeature = 4;
    base.OnElementChanged(e);

    if (e.OldElement != null || Element == null)
    {
        return;
    }

    //Switch back to the Wheels style for the datePicker (in iOS 14 the default of Automatic uses the Compact style)
    if (UIDevice.CurrentDevice.CheckSystemVersion(iosVersionMajorForDatePickerStyleFeature, iosVersionMinorForDatePickerStyleFeature)
        && Control?.InputView is UIDatePicker datePicker && datePicker.PreferredDatePickerStyle != UIDatePickerStyle.Wheels)
    {
        datePicker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels;
        datePicker.SizeToFit();
    }

    //Do whatever other customization is desired
    //UpdateBorderColor();
    //UpdateBorderWidth();
    //UpdateCornerRadius();
    //UpdatePadding();
}

【讨论】:

    【解决方案4】:

    我最近也遇到了这个有趣的问题。我们用Xcode 12打包的时候一切正常,但是切换到Xcode 12及以上版本打包日期选择和显示就出现异常了。

    因为 iOS 13.4 UIDatePicker 有了新功能

    • @property (nonatomic, readwrite, assign) UIDatePickerStyle preferredDatePickerStyle API_AVAILABLE(ios(13.4)) API_UNAVAILABLE(tvos, watchos);

    如果你想保持原来的风格,你可以添加这段代码

       _datePick = [[UIDatePicker alloc] init];
       if (@available(iOS 13.4, *)) {
           _datePick.preferredDatePickerStyle = UIDatePickerStyleWheels;
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 2021-05-27
      • 2019-03-30
      • 1970-01-01
      • 2021-01-08
      • 2018-02-22
      • 1970-01-01
      相关资源
      最近更新 更多