我需要更改取消按鈕的文本,以在我的應用程序中始終顯示「重置」。 我在這裏發現了很多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取消按鈕的文本顏色
- 4. 更改取消按鈕BackgroundColor和UISearchBar在iOS7中的文本
- 5. 的UISearchBar取消按鈕
- 6. UISearchBar裏面取消按鈕
- 7. UISearchBar取消按鈕Xcode 4.3?
- 8. 無法更改UISearchBar中的取消按鈕的標題
- 9. 更改文字後,無法更改UISearchBar取消按鈕標題顏色。
- 10. 更改UISearchBar的字體樣式取消按鈕
- 11. 如何更改Swift 3中UISearchBar「取消」按鈕的顏色?
- 12. 如何更改iOS7中UISearchBar的取消按鈕色調顏色
- 13. 如何更改UISearchBar中取消按鈕的顏色
- 14. 圖片爲取消的UISearchBar的按鈕
- 15. 取消按鈕解僱的UISearchBar鍵盤
- 16. 的UISearchBar barTintColor取消按鈕不可見
- 17. 上的取消按鈕按下的UISearchBar的 - 在iPhone
- 18. Swift:修改UISearchBar取消按鈕字體的文本顏色和樣式
- 19. UISearchBar:如何防止取消按鈕清除文本?
- 20. iPhone - 更改UISearchDisplayController「取消」按鈕文字
- 21. 按鈕消失當textview文本更改
- 22. UISearchBar取消按鈕沒有響應
- 23. Iphone UISearchBar取消按鈕故障
- 24. 更改MFMailComposeViewController上的發送/取消按鈕上文本的顏色
- 25. 更改NSOpenPanel(Cocoa OSX)的取消按鈕的文本
- 26. 更改按鈕上文本的方向
- 27. 更改默認TouchID對話框中取消按鈕的文本
- 28. 如何更改消息框按鈕上的文本?
- 29. 使用php更改html按鈕文本
- 30. UIActivityViewController - 如何更改取消按鈕文本
這將是更好,如果你能提供的一個答案(你可以回答自己的問題在於是,看見的FAQ)並將其標記爲回答 - 即幫助人們尋找答案。 – poupou 2013-02-28 13:24:40