Change text of “Cancel” button on a UISearchBar us

2019-07-19 07:19发布

I need to change the text of the Cancel button to always display "Reset" in my app. I found many similar questions here on SO, but all answers are for ObjC, while I am working in Monotouch.

3条回答
姐就是有狂的资本
2楼-- · 2019-07-19 07:59

Note that the subview hierarchy is different in ios6 and ios7.

But by adding the extension method below you can use

searchBar.SetCancelTitle("NO!");

Extension method that works in ios7 and ios5/ios6:

public static class NimbleSearchBarExtensions
{
    /// <summary>
    /// Finds the Cancelbutton
    /// </summary>
    /// <returns></returns>
    public static UIButton GetCancelButton(this UISearchBar searchBar)
    {
        //Look for a button, probably only one button, and that is probably the cancel button.
        return searchBar.GetAllSubViews().OfType<UIButton>().FirstOrDefault();
    }

    /// <summary>
    /// Recursively traverses all subviews and returns them in a little list.
    /// </summary>
    /// <param name="view"></param>
    /// <returns></returns>
    public static IEnumerable<UIView> GetAllSubViews(this UIView view)
    {
        List<UIView> retList = new List<UIView>();
        retList.AddRange(view.Subviews);
        foreach (var subview in view.Subviews)
        {
            retList.AddRange(subview.GetAllSubViews());
        }

        return retList;
    }

    /// <summary>
    /// Sets the title of the search bars cancel button
    /// </summary>
    /// <param name="searchBar"></param>
    /// <param name="cancelTitle"></param>
    public static void SetCancelTitle(this UISearchBar searchBar, string cancelTitle)
    {
        searchBar.GetCancelButton().SetTitle(cancelTitle,UIControlState.Normal);
    }
}
查看更多
仙女界的扛把子
3楼-- · 2019-07-19 08:01

Use the extension method below for iOS7+ like this: searchBar.SetCancelTitle("close");

It derives from Kolbjørn Bredrup's comment, which is great, but it wasn't clear to me what the namespaces were for the additional extension methods that it uses internally (OfType and FirstOrDefault) so I added them to this class myself.

I also renamed the class UISearchBarExtensions so that it sat nicely with my other ones.

Thanks to Kolbjørn Bredrup for the original!

As with the original, it is called using:

searchBar.SetCancelTitle("close");

public static class UISearchBarExtensions
{
    /// <summary>
    /// Finds the Cancelbutton
    /// </summary>
    /// <returns></returns>
    public static UIButton GetCancelButton(this UISearchBar searchBar)
    {
        //Look for a button, probably only one button, and that is probably the cancel button.
        return (UIButton)searchBar.GetAllSubViews().OfType<UIButton>().FirstOrDefault();
    }

    public static UIView FirstOrDefault(this IEnumerable<UIView> views)
    {
        return ((List<UIView>)views)[0];
    }

    public static IEnumerable<UIView> OfType<T>(this IEnumerable<UIView> views)
    {
        List<UIView> response = new List<UIView>();
        foreach(var view in views)
        {
            if(view.GetType() == typeof(T))
            {
                response.Add(view);
            }
        }
        return response;
    }

    /// <summary>
    /// Recursively traverses all subviews and returns them in a little list.
    /// </summary>
    /// <param name="view"></param>
    /// <returns></returns>
    public static IEnumerable<UIView> GetAllSubViews(this UIView view)
    {
        List<UIView> retList = new List<UIView>();
        retList.AddRange(view.Subviews);
        foreach (var subview in view.Subviews)
        {
            retList.AddRange(subview.GetAllSubViews());
        }

        return retList;
    }

    /// <summary>
    /// Sets the title of the search bars cancel button
    /// </summary>
    /// <param name="searchBar"></param>
    /// <param name="cancelTitle"></param>
    public static void SetCancelTitle(this UISearchBar searchBar, string cancelTitle)
    {
        searchBar.GetCancelButton().SetTitle(cancelTitle, UIControlState.Normal);
    }
}
查看更多
做自己的国王
4楼-- · 2019-07-19 08:11

It was very easy -

UIButton btnCancel = (UIButton)SearchBar.Subviews[3];
btnCancel.SetTitle ("test", UIControlState.Normal);
查看更多
登录 后发表回答