使用Monotouch更改UISearchBar上的“取消”按钮的文本?

时间:2013-02-28 07:49:20

标签: c# xamarin.ios uisearchbar xamarin

我需要更改“取消”按钮的文本,以便始终在我的应用中显示“重置”。 我在这里发现了很多similar个问题,但所有答案都是针对ObjC的,而我在Monotouch工作。

3 个答案:

答案 0 :(得分:2)

对iOS7 +使用以下扩展方法,如下所示:searchBar.SetCancelTitle(“close”);

它来自KolbjørnBredrup的评论,这很好,但是我不清楚它在内部使用的额外扩展方法(OfType和FirstOrDefault)的命名空间是什么,所以我自己将它们添加到这个类中。 / p>

我还重命名了UISearchBarExtensions类,以便与其他类完美搭配。

感谢KolbjørnBredrup的原创作品!

与原版一样,使用:

调用

searchBar.SetCancelTitle( “关闭”);

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);
    }
}

答案 1 :(得分:1)

请注意,ios6和ios7中的子视图层次结构不同。

但是通过添加下面的扩展方法,您可以使用

searchBar.SetCancelTitle("NO!");

适用于ios7和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);
    }
}

答案 2 :(得分:0)

这很容易 -

UIButton btnCancel = (UIButton)SearchBar.Subviews[3];
btnCancel.SetTitle ("test", UIControlState.Normal);