locked
Picker with additional text? RRS feed

  • Question

  • User241511 posted

    Hi guys,

    I really would like to show additional text while using the picker, may be left from "done", is that possible? See screenshot

    Thank you and best regards Chris

    Thursday, September 5, 2019 7:41 AM

Answers

  • User369978 posted

    You need custom renderer , the white area is InputAccessoryView which you could add subview on it .

    Code

    [assembly: ExportRenderer(typeof(Picker), typeof(MyRenderer))]
    namespace FormsApp.iOS
    {
        class MyRenderer : PickerRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
            {
                base.OnElementChanged(e);
    
                if(Control != null)
                {
                    UITextField textField = Control;
    
                    UIView view = textField.InputAccessoryView;
                    UILabel label = new UILabel();
                    label.Frame = view.Frame;
                    label.Text = "Test";
    
                    view.Add(label);
    
                    textField.InputAccessoryView = view;
                }
    
            }
        }
    }
    

    Screen shot

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, September 5, 2019 11:04 AM

All replies

  • User369978 posted

    You need custom renderer , the white area is InputAccessoryView which you could add subview on it .

    Code

    [assembly: ExportRenderer(typeof(Picker), typeof(MyRenderer))]
    namespace FormsApp.iOS
    {
        class MyRenderer : PickerRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
            {
                base.OnElementChanged(e);
    
                if(Control != null)
                {
                    UITextField textField = Control;
    
                    UIView view = textField.InputAccessoryView;
                    UILabel label = new UILabel();
                    label.Frame = view.Frame;
                    label.Text = "Test";
    
                    view.Add(label);
    
                    textField.InputAccessoryView = view;
                }
    
            }
        }
    }
    

    Screen shot

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, September 5, 2019 11:04 AM
  • User241511 posted

    That's it! And so easy ;) Thank you!!!

    Any chance to change the text color of "Fertig" (Done)? And may be add an Cancel Button? Didn't find anything yet :(

    Best regards Chris

    Friday, September 6, 2019 6:06 AM
  • User241511 posted

    I found out to change color of "Done" with view.TintColor ;) But any chance for a cancel button?

    Friday, September 6, 2019 6:47 AM
  • User369978 posted

    You could customize everything on the InputAccessoryView , but you should create a new UIView instead .

      if(Control != null)
            {
                UITextField textField = Control;
    
                //new a UIView
                UIView view = new UIView(frame:new CGRect(0,0,UIScreen.MainScreen.Bounds.Width, 40));
                view.BackgroundColor = UIColor.FromRGB(new nfloat(200), new nfloat(200), new nfloat(200));
    
                //top border
                CALayer topBorder = CALayer.Create();
                topBorder.Frame = new CGRect(0, 0, view.Frame.Width, 1);
                topBorder.BackgroundColor = UIColor.Gray.CGColor;
                view.Layer.AddSublayer(topBorder);
    
                // left label
                UILabel label = new UILabel();
                label.Frame = view.Frame;
                label.Text = "Test";
                view.Add(label);
    
    
                //right button
                UIButton button = new UIButton();
                button.Frame = new CGRect(view.Frame.Width -70, 0, 70, 40);
                button.SetTitle("Cancel", UIControlState.Normal);
                button.SetTitleColor(UIColor.Green, UIControlState.Normal);
                button.TouchUpInside += (sender, e1) =>
                {
                    textField.ResignFirstResponder();
                };
                view.Add(button);
    
                textField.InputAccessoryView = view;
            }
    

    Friday, September 6, 2019 8:12 AM