Search code examples
iosuibuttonuiappearancexamarin.ios

Cannot change the title of the cancel button in the UISearchBar


I would like to change the title of the cancel button of the UISearchBar using MonoTouch. Yes I have seen this "hack" but I prefer a more elegant way. I am trying to use:

UIButton.AppearanceWhenContainedIn(typeof(UISearchBar)).SetTitle("xxxx");

which it is supposed to work in objective-c but in MonoTouch I get the following error

message: Error CS1061: Type MonoTouch.UIKit.UIButton.UIButtonAppearance' does not contain a definition forSetTitle' and no extension method SetTitle' of type MonoTouch.UIKit.UIButton.UIButtonAppearance' could be found (are you missing a using directive or an assembly reference?)

I am using Xamarin Studio ver. 4.0


Solution

  • Why not use a bit more lightweight hack?

        public static void SetSearchBarCancelButtonTitle (UISearchBar sb, string title)
        {
            foreach (var subview in sb.Subviews)
                if (subview is UIButton)
                    (subview as UIButton).SetTitle (title, UIControlState.Normal);
        }