iOS 14个DatePicker渲染器
iOS 14 DatePicker renderer
我在弄清楚如何正确更新我现有的自定义渲染器时遇到了一些困难,我在 iOS 上为我的 DatePicker 使用了不同的首选样式来显示 DatePicker,如本文此处所述(尽管它适用于 swift) https://medium.com/better-programming/introducing-swifts-new-modern-date-picker-37bb5e0a106
我的渲染器如下:
public class BorderlessDatePickerRenderer : DatePickerRenderer
{
public override void LayoutSubviews()
{
base.LayoutSubviews();
var element = Element as BorderlessDatePicker;
Control.BorderStyle = UITextBorderStyle.None;
if (element.Date.Year == 1900) {
Control.Text = "";
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var element = Element as BorderlessDatePicker;
if (Control != null && element.Date.Year == 1900) {
Control.Text = "";
}
}
BorderlessDatePicker
本身只是一个扩展 DatePicker Xamarin.Forms 控件的空 class。我的问题的根源在于我不确定如何在我的 Control 对象上正确设置 PreferredDatePickerStyle,因为 Control 是引擎盖下的 UITextField 而不是 UIDatePicker。本质上,我想做的不是使用 compact
样式显示日期选择器,这似乎是 iOS 14 的默认样式,我希望它显示为 wheels
而不是做类似的事情:
PreferredDatePickerStyle = UIDatePickerStyle.Wheels;
经过更多研究和浏览 xamarin github,我找到了这个解决方案:
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged(e);
if(e.NewElement != null && this.Control != null)
{
try
{
if (UIDevice.CurrentDevice.CheckSystemVersion(14, 0))
{
UIDatePicker picker = (UIDatePicker)Control.InputView;
picker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels;
}
} catch (Exception ex)
{
Log.Error(ex, "Failed to set PreferredDatePickerStyle to be UIDatePickerStyle.Wheels for iOS 14.0+");
}
}
}
我在弄清楚如何正确更新我现有的自定义渲染器时遇到了一些困难,我在 iOS 上为我的 DatePicker 使用了不同的首选样式来显示 DatePicker,如本文此处所述(尽管它适用于 swift) https://medium.com/better-programming/introducing-swifts-new-modern-date-picker-37bb5e0a106
我的渲染器如下:
public class BorderlessDatePickerRenderer : DatePickerRenderer
{
public override void LayoutSubviews()
{
base.LayoutSubviews();
var element = Element as BorderlessDatePicker;
Control.BorderStyle = UITextBorderStyle.None;
if (element.Date.Year == 1900) {
Control.Text = "";
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var element = Element as BorderlessDatePicker;
if (Control != null && element.Date.Year == 1900) {
Control.Text = "";
}
}
BorderlessDatePicker
本身只是一个扩展 DatePicker Xamarin.Forms 控件的空 class。我的问题的根源在于我不确定如何在我的 Control 对象上正确设置 PreferredDatePickerStyle,因为 Control 是引擎盖下的 UITextField 而不是 UIDatePicker。本质上,我想做的不是使用 compact
样式显示日期选择器,这似乎是 iOS 14 的默认样式,我希望它显示为 wheels
而不是做类似的事情:
PreferredDatePickerStyle = UIDatePickerStyle.Wheels;
经过更多研究和浏览 xamarin github,我找到了这个解决方案:
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged(e);
if(e.NewElement != null && this.Control != null)
{
try
{
if (UIDevice.CurrentDevice.CheckSystemVersion(14, 0))
{
UIDatePicker picker = (UIDatePicker)Control.InputView;
picker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels;
}
} catch (Exception ex)
{
Log.Error(ex, "Failed to set PreferredDatePickerStyle to be UIDatePickerStyle.Wheels for iOS 14.0+");
}
}
}