1

There are lots of objective-c variations of how to access the UITextField in a UISearchbar so you can style it more specifically than the exposed SearchBar methods, i.e. we want to change - 

  • font type / size
  • font colour
  • text field background colour so searchbar is all one colour

Thought it would be nice to see Xamarin solution(s) for this as certain obj-c techniques do not seem (I may just be missing the code) to translate -

For example, in -

Change the font size and font style of UISearchBar iOS 7

- (void)viewDidLoad
{

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
        NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20],
  }];

}

Looks like a winner, but not sure can translate this to Xamarin as properties not "seemingly" exposed -

UITextField.AppearanceWhenContainedIn(typeof (UISearchBar))).HowToSetFont ...

I will make a starter answer for one which I have just come across, but interested to see how other users have solved styling the searchbar too.

Community
  • 1
  • 1
WickedW
  • 2,331
  • 4
  • 24
  • 54

1 Answers1

4
_filterSearchBar = new UISearchBar
{
    BarTintColor = UIColor.Red,     // We want a red search bar with text field background same as outer "margin"
    TintColor = UIColor.White,
    SearchBarStyle = UISearchBarStyle.Default,
    BackgroundImage = UIImage.FromFile("transparent1x1.png")     // "trick" to ensure background is clean  
};

// Main Part - Use KVO to access text field
var uiTextField = (UITextField)_filterSearchBar.ValueForKey(new NSString("_searchField"));

uiTextField.Font = YourFontHere;
uiTextField.TextColor = UIColor.White;
uiTextField.BackgroundColor = UIColor.Red;            

// Placeholder colour is white 
uiTextField.AttributedPlaceholder = new NSAttributedString("SearchBarPlaceholder", null, UIColor.White);

// We still have not sorted spyglass icon colour et al to white but can try set icon image later
valdetero
  • 4,624
  • 1
  • 31
  • 46
WickedW
  • 2,331
  • 4
  • 24
  • 54