雖然這可能不是原來的問題正好相應的解決方案仍然是適用於試圖定製的的UISearchBar取消按鈕的更大意義。認爲這將有助於陷入這種情況的其他人。
我的情況是改變取消按鈕的標題,但有一個轉折點,其中我不想在默認情況下顯示取消按鈕,但只希望顯示,當用戶進入搜索模式時(通過點擊裏面搜索文本字段)。在這個瞬間,我想要取消按鈕帶有「完成」標題(「取消」給我的屏幕不同的含義,因此定製)。
儘管如此,這裏是我做了什麼(caelavel的和Arenim的解決方案的組合):
子類的UISearchBar作爲MyUISearchBar這兩種方法:
-(void) setCloseButtonTitle: (NSString *) title forState: (UIControlState)state
{
[self setTitle: title forState: state forView:self];
}
-(void) setTitle: (NSString *) title forState: (UIControlState)state forView: (UIView *)view
{
UIButton *cancelButton = nil;
for(UIView *subView in view.subviews){
if([subView isKindOfClass:UIButton.class])
{
cancelButton = (UIButton*)subView;
}
else
{
[self setTitle:title forState:state forView:subView];
}
}
if (cancelButton)
[cancelButton setTitle:title forState:state];
}
而且在使用這個搜索欄的視圖 - 控制時,代碼如下一段需要顯示的取消按鈕和定製其標題的護理:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
MyUISearchBar *sBar = (MyUISearchBar *)searchBar;
[sBar setShowsCancelButton:YES];
[sBar setCloseButtonTitle:@"Done" forState:UIControlStateNormal];
}
奇怪的是,我沒有哈我們有辦法隱藏取消按鈕,因爲它在默認情況下隱藏,當搜索模式退出時。
爲什麼不在這裏嵌入圖像? – 2017-03-25 08:03:03