gpt4 book ai didi

c# - 使用 Monotouch 更改 UISearchBar 上 “Cancel” 按钮的文本?

转载 作者:行者123 更新时间:2023-11-30 22:20:07 25 4
gpt4 key购买 nike

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

最佳答案

对 iOS7+ 使用如下扩展方法:searchBar.SetCancelTitle("close");

它源自 Kolbjørn Bredrup 的评论,这很棒,但我不清楚它在内部使用的其他扩展方法(OfType 和 FirstOrDefault)的 namespace 是什么,所以我自己将它们添加到此类。

我还重命名了 UISearchBarExtensions 类,以便它与我的其他类一起使用。

感谢 Kolbjørn Bredrup 的原创!

和原来一样,叫using:

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

关于c# - 使用 Monotouch 更改 UISearchBar 上 “Cancel” 按钮的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15130180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com