2

我需要更改取消按钮的文本以始终在我的应用程序中显示“重置”。我在 SO 上发现了许多类似的问题,但所有答案都是针对 ObjC,而我在 Monotouch 工作。

4

3 回答 3

2

像这样为 iOS7+ 使用下面的扩展方法:searchBar.SetCancelTitle("close");

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

我还重命名了 UISearchBarExtensions 类,以便它与我的其他类很好地配合。

感谢 Kolbjørn Bredrup 的原创!

与原来的一样,它被称为使用:

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);
    }
}
于 2015-09-02T10:59:50.823 回答
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);
    }
}
于 2014-02-28T14:58:54.073 回答
0

这很容易 -

UIButton btnCancel = (UIButton)SearchBar.Subviews[3];
btnCancel.SetTitle ("test", UIControlState.Normal);
于 2013-03-01T07:12:48.827 回答