2016-09-02 44 views
10

我有這樣的問題:Hide UISearchBar Cancel Button如何覆蓋UISearchController的子類中的「searchBar」屬性?

我已經子類UISearchControllerUISearchBar,並覆蓋方法layoutSubviewsUISearchBar

如何覆蓋searchBar屬性與自定義UISearchBar在我的自定義UISearchController

更新1

要實現自定義UISearchBar財產我做MyUISearchController

@property(nonatomic, strong, readonly) UISearchBar *searchBar; 

而且

@synthesize searchBar=_searchBar; 

- (UISearchBar *)searchBar { 
    return [MySearchBar new]; 
} 

UISearchController使用默認searchBar反正..

更新

h文件:

@interface MySearchController : UISearchController <UITableViewDelegate, UITableViewDataSource> 

m文件:

@interface MySearchController() <UISearchDisplayDelegate, UISearchControllerDelegate, 
UISearchBarDelegate, UISearchResultsUpdating, SearchAutocompleteLoader> 

@property UISearchController *searchController; 

@property (strong, nonatomic) UITableView *searchResultTable; 
@property UIView *viewForSearchBar; 
@property (nonatomic) NSInteger filterSettings; 
@property (strong, nonatomic) UILabel *matchesLabel; 
@property (strong, nonatomic) UIView *loading; 
@property (strong, nonatomic) UIView *foggView; 

@end 

- (instancetype)initWith:(UIView *)viewForSearch foggView:(UIView *)view delegate:(id)delegate andResultTableView:(UITableView *)tableView 
{ 
    self.viewForSearchBar = viewForSearch; 
    self.searchResultTable = tableView; 
    self.foggView = view; 
    self.searchDelegate = delegate; 
    [self configureSearchController]; 
    return self; 
} 

- (void)configureSearchController { 
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 

    self.searchController.searchResultsUpdater = self; 
    self.searchController.delegate = self; 
    self.searchController.hidesNavigationBarDuringPresentation = NO; 
    self.searchController.dimsBackgroundDuringPresentation = NO; 
    [self loadFilterSettings]; 
    self.searchController.searchBar.delegate = self; 
    [self.searchController.searchBar setShowsCancelButton:NO]; 
    //self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault; 

    [self.searchController.searchBar setBackgroundImage:[UIImage imageWithCGImage:(__bridge CGImageRef)([UIColor clearColor])]]; 
    [self.searchController.searchBar setImage:[UIImage imageNamed:@"iconSearchSettings"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal]; 
    self.searchController.searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, self.viewForSearchBar.frame.size.height); 
    self.viewForSearchBar.tintColor = [UIColor yellowColor]; 
    // [self.viewForSearchBar addSubview:self.searchController.searchBar]; 

    self.searchController.searchBar.barTintColor = [UIColor whiteColor]; 
    self.searchController.searchBar.tintColor = [UIColor blackColor]; 
    //self.searchController.searchBar.backgroundColor = [UIColor whiteColor]; 

    [self.viewForSearchBar addSubview:self.searchController.searchBar]; 

    for (UIView *subView in self.searchController.searchBar.subviews) { 
     if ([subView isKindOfClass:[UITextField class]]) { 
      [subView setTintColor:[UIColor blueColor]]; 
      [[(UITextField *) subView valueForKey:@"textInputTraits"] setValue:[UIColor blueColor] forKey:@"insertionPointColor"]; 
     } 
    } 

    [self.searchController setActive:YES]; 
    [self.searchController setActive:NO]; 
} 
+0

躲在取消按鈕子類'searchBar'的唯一原因? –

+0

@WarifAkhandRishi是的 – Gikas

+0

@Gikas請發表一些代碼,以獲得更好的理解。否則很難找出問題。解決這個問題的辦法是隱藏取消按鈕在viewsWillLayoutSubviews的視圖控制器 –

回答

3

UISearchController的搜索欄是一個計算的屬性,你必須保存您的自定義搜索欄並在致電searchBar時將其退回。

這裏是斯威夫特一些代碼:

var mySearchBar = MySearchBar() 

override var searchBar: UISearchBar { 
    get{ 
     return mySearchBar; 
    } 
} 
0
import UIKit 

class MySearchController: UISearchController { 

    override func viewWillLayoutSubviews() { 
     super.viewWillLayoutSubviews() 
     searchBar.showsCancelButton = false 
    } 
} 
+0

我看到了這個解決方案http://stackoverflow.com/questions/8496310/hide-uisearchbar-cancel-button 但這不工作.. – Gikas

+0

@Gikas它正在我的演示項目中工作。 –

+0

我已更新該問題 – Gikas

1

爲了自定義搜索欄屬性通過繼承第一搜索開始定製過程酒吧(UISearchBar)。然後,在搜索控制器的子類中使用此自定義搜索欄,最後在您的ViewController類中使用它們兩者。

參見本教程: UISearchController Customization

0

見這個教程定製的UISearchBar斯威夫特:

Customise UISearchBar

的Objective - C:在viewDidLoad中添加代碼。你已經申報的UISearchBar

[searchBar setShowsCancelButton:NO]; 

對象斯威夫特:在viewDidLoad中

searchBar.showsCancelButton = false 

Image