Вспомогательное представление ввода в Xamarin ios (формы) в PickerRenderer - PullRequest
0 голосов
/ 27 февраля 2020

Accessory View With Title

Как добавить заголовок «Состояние» в середине панели инструментов для PickerRenderer в Xamarin ios (формы)?

1 Ответ

0 голосов
/ 28 февраля 2020

Вы можете проверить следующий код

using System;
using xxx.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(Picker), typeof(MyiOSPickerRenderer))]
namespace xxx.iOS
{
    public class MyiOSPickerRenderer : PickerRenderer, IUIPickerViewDelegate, IUIPickerViewDataSource
    {

        string SelectedValue;

        public MyiOSPickerRenderer()
        {

        }

        public nint GetComponentCount(UIPickerView pickerView)
        {
            return 1;
        }

        public nint GetRowsInComponent(UIPickerView pickerView, nint component)
        {
            return Element.Items.Count;
        }

        [Export("pickerView:viewForRow:forComponent:reusingView:")]
        public UIView GetView(UIPickerView pickerView, nint row, nint component, UIView view)
        {




            UILabel label = new UILabel
            {
                //here you can set the style of item!!!

                TextColor = UIColor.Blue,

                Text = Element.Items[(int)row].ToString(),

                TextAlignment = UITextAlignment.Center,

            };
            return label;
        }


        [Export("pickerView:didSelectRow:inComponent:")]
        public void Selected(UIPickerView pickerView, nint row, nint component)
        {
            Control.Text = Element.Items[(int)row];
            SelectedValue = Element.Items[(int)row];
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                SelectedValue = Element.Items[0];

                UIPickerView pickerView = (UIPickerView)Control.InputView;
                pickerView.WeakDelegate = this;
                pickerView.DataSource = this;


                UIToolbar toolbar = (UIToolbar)Control.InputAccessoryView;

                UIBarButtonItem save = new UIBarButtonItem("Save", UIBarButtonItemStyle.Done, (object sender, EventArgs click) =>
                {
                    Control.Text = SelectedValue;
                    toolbar.RemoveFromSuperview();
                    pickerView.RemoveFromSuperview();
                    Control.ResignFirstResponder();
                });

                UIBarButtonItem Title = new UIBarButtonItem("States", UIBarButtonItemStyle.Done, null);


                UIBarButtonItem cancel = new UIBarButtonItem("Cancel", UIBarButtonItemStyle.Bordered, (object sender, EventArgs click) =>
                {

                    toolbar.RemoveFromSuperview();
                    pickerView.RemoveFromSuperview();
                    Control.ResignFirstResponder();
                });
                UIBarButtonItem empty = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);
                toolbar.Items = new UIBarButtonItem[] { cancel, empty, Title, empty, save };
            }

        }
    }
}

enter image description here

...