繁体   English   中英

iOS Xamarin.Forms的Picker渲染器?

[英]Picker renderer for iOS Xamarin.Forms?

在此处输入图片说明

我是Xamarin.Forms编程的新手。 想自定义/更改选择器的方式

在图片中:-带有文本“选择”的按钮,在单击时调用picker.Focus()-在选择按钮后面同时具有黑色背景和文本颜色的选择器-空的ListView-选择器处于选中状态时弹出选择器选项轮窗格“专注”

选择器实现代码:

Picker picker = new Picker
{
    HorizontalOptions = LayoutOptions.Start,
    VerticalOptions = LayoutOptions.Center,
    WidthRequest = 73,
    HeightRequest = 25,
    BackgroundColor = Color.Black,
    TextColor = Color.Black
};

List<string> myList = new List<string>();
myList.Add("OPTIONS 1");
myList.Add("OPTIONS 2");
myList.Add("OPTIONS 3");
myList.Add("OPTIONS 4");
myList.Add("OPTIONS 5");

foreach (string str in myList)
{
    picker.Items.Add(str);
}

代码自定义了用户单击以使选择器选项轮窗格进入视图的“框”,但是没有(或至少看起来没有)提供用于自定义选择器窗格的任何选项。

如何将Picker Wheel弹出窗格放置在ListView内? 如何更改拨轮内部选项的字体大小以及如何更改显示选项的窗格的高度?

tl; dr-如何更改选择器窗格的高度,项目字体大小,窗格的位置?

据我所知...

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using AppName.iOS;
using AppName;

[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
namespace AppName.iOS
{
    public class MyPickerRenderer : PickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                // Unsubscribe from event handlers and cleanup any resources
            }

            if (e.NewElement != null)
            {
                // Configure the native control and subscribe to event handlers
            }
        }
    }
}

iOS中的拾轮器称为UIPickerView我们可以在渲染器中获得此控制,例如:

UITextField textField = Control;
UIPickerView pickerView = textField.InputView as UIPickerView;

然后,我们可以根据需要使用Delegate进行自定义。

首先,在您的情况下,我们应该获取dataSource ,如下所示:

Picker myPicker = Element;
itemList = myPicker.Items.ToList();

其次,创建委托并将列表设置为参数:

pickerView.Delegate = new MyPickerViewDelegate(itemList);

public class MyPickerViewDelegate: UIPickerViewDelegate
{

    List<string> itemList;

    public MyPickerViewDelegate(List<string> list)
    {
        itemList = list;
    }
}

最后,我们可以使用以下事件开始自定义:

//Define the Font size or style
public override NSAttributedString GetAttributedTitle(UIPickerView pickerView, nint row, nint component)
{
    var text = new NSAttributedString(
        itemList[(int)row],
        font: UIFont.SystemFontOfSize(24),
        foregroundColor: UIColor.Red,
        strokeWidth: 4
    );

    return text;
}
//Define the row height
public override nfloat GetRowHeight(UIPickerView pickerView, nint component)
{
    return 80;
}

此外,如果要自定义更灵活(包括放置),可以使用以下方法:

public override UIView GetView(UIPickerView pickerView, nint row, nint component, UIView view)
{
    UIView contentView = new UIView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 80));

    UILabel label = new UILabel();
    label.Frame = contentView.Bounds;
    contentView.AddSubview(label);

    label.Text = itemList[(int)row];
    //Change the label style
    label.Font = UIFont.SystemFontOfSize(24);
    label.TextColor = UIColor.Red;

    return contentView;
}

这是我自定义的渲染器,供您参考:

public class MyPickerRenderer : PickerRenderer
{
    List<string> itemList;
    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);

        Picker myPicker = Element;
        itemList = myPicker.Items.ToList();

        UITextField textField = Control;
        UIPickerView pickerView = textField.InputView as UIPickerView;
        pickerView.Delegate = new MyPickerViewDelegate(itemList);
    }
}

您将必须创建自己的UiPickerView 这样您就可以使用本指南或本指南来设置字体大小。 至于高度和位置; 看来您可以设置位置,但是高度要困难一些,我只能在目标C或swift中找到示例。

祝你好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM