我需要更改取消按鈕的文本,以在我的應用程序中始終顯示「重置」。 我在這裏發現了很多similar問題,但所有的答案都是針對ObjC的,而我在Monotouch中工作。使用Monotouch更改UISearchBar上「取消」按鈕的文本?
回答
這是非常容易的 -
UIButton btnCancel = (UIButton)SearchBar.Subviews[3];
btnCancel.SetTitle ("test", UIControlState.Normal);
這不適用於iOS 7 – 2015-01-11 07:02:09
注意,子視圖層次是在iOS6的和ios7不同。
但通過添加以下你的擴展方法可以使用,在ios7和工作的ios5/iOS6的
searchBar.SetCancelTitle("NO!");
擴展方法:
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);
}
}
使用下面的iOS7 +這樣的擴展方法:搜索欄.SetCancelTitle( 「關閉」);
它來源於KolbjørnBredrup的評論,這很棒,但我不清楚它內部使用的額外擴展方法(OfType和FirstOrDefault)的名稱空間是什麼,所以我將它們添加到了這個類中。
我也重命名類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);
}
}
「我不清楚它在內部使用的附加擴展方法的命名空間是什麼」 - 不需要所有自定義方法 - 只需使用:'使用System.Linq; – jbyrd 2017-08-21 13:55:53
- 1. 如何更改UISearchBar的取消按鈕的文本顏色?
- 2. 的UISearchBar取消按鈕
- 3. UISearchBar取消按鈕Xcode 4.3?
- 4. 無法更改UISearchBar中的取消按鈕的標題
- 5. 更改UISearchBar的字體樣式取消按鈕
- 6. 如何更改Swift 3中UISearchBar「取消」按鈕的顏色?
- 7. 如何更改UISearchBar中取消按鈕的顏色
- 8. 取消按鈕解僱的UISearchBar鍵盤
- 9. 上的取消按鈕按下的UISearchBar的 - 在iPhone
- 10. iPhone - 更改UISearchDisplayController「取消」按鈕文字
- 11. Iphone UISearchBar取消按鈕故障
- 12. UISearchBar取消按鈕沒有響應
- 13. 造型取消按鈕在UISearchBar
- 14. 更改MFMailComposeViewController上的發送/取消按鈕上文本的顏色
- 15. 更改NSOpenPanel(Cocoa OSX)的取消按鈕的文本
- 16. 更改按鈕上文本的方向
- 17. UIActivityViewController - 如何更改取消按鈕文本
- 18. 如何更改UIAlertView取消按鈕文本
- 19. 如何使用Android上的EditText更改按鈕的文本
- 20. 更改UISearchBar搜索按鈕上的UITableView數據源點擊
- 21. 使用jquery mobile更改按鈕上的文本。
- 22. 更改文本onclick按鈕
- 23. Appcelerator更改按鈕文本
- 24. Silverlight更改按鈕文本
- 25. 顯示的UISearchBar「X」文本字段中與相鄰的取消按鈕
- 26. MonoTouch整合Facebook取消按鈕無法使用
- 27. 當鍵盤在iPad上消失時,UISearchBar取消按鈕被禁用
- 28. 使用jquery將按鈕更改爲圖片取決於文本?
- 29. 使用SearchBarDisplay時,如何更改取消按鈕的名稱
- 30. 在其他按鈕上更改按鈕文本android
這將是更好,如果你能提供的一個答案(你可以回答自己的問題在於是,看見的FAQ)並將其標記爲回答 - 即幫助人們尋找答案。 – poupou 2013-02-28 13:24:40