Karthik Raja AKR
Karthik Raja AKR

Reputation: 93

How do I change UIPickerView text color in renderer view using Xamarin.Forms.iOS?

I have used UIPickerView in ios renderer file.I tried to change text and background color of view. Used SetValueForKeyPath override method to change text color. While picker initially loaded into view text color was not changed but it does when I change selected index(Problem with initial loading).

protected override void OnElementChanged(ElementChangedEventArgs<CustomGrid> e)
{
   if (e.NewElement != null)
   {
      var nativeView = new UIPickerView();
      nativeView.Model = new PickerSource();
      nativeView.BackgroundColor = UIColor.Yellow;
      nativeView.SetValueForKeyPath(UIColor.Red, (NSString)"textColor");
      CGRect cGRect = new CGRect(0, 0, 100, 100);
      SetNativeControl(nativeView);
   }

   base.OnElementChanged(e);
}

Upvotes: 3

Views: 678

Answers (1)

Brandon Minnick
Brandon Minnick

Reputation: 15340

I advise against using a PickerRenderer to change the text color, because the Xamarin.Forms.Platform.iOS.PickerRenderer is very complex; it's actually a UIToolbar, UIBarButtonItem and a UITextField combined into a UIPickerView.

The code for Xamarin.Forms.Platform.iOS.PickerRenderer is open-source if you'd like to see how the Xamarin.Forms team initialized a Picker on iOS.

Xamarin.Forms.Picker

It is much easier to set the TextColor in Xamarin.Forms:

var picker = new Xamarin.Forms.Picker
{
    TextColor = Color.Red
};

Upvotes: 0

Related Questions