2016-09-02 15 views
10

Tôi có vấn đề như thế này: Hide UISearchBar Cancel ButtonCách ghi đè thuộc tính "searchBar" trong phân lớp của UISearchController?

Tôi đã subclassed UISearchControllerUISearchBar, và ghi đè phương pháp layoutSubviews trong UISearchBar.

Cách ghi đè searchBar thuộc tính với tùy chỉnh UISearchBar trong tùy chỉnh UISearchController của tôi?

Cập nhật 1

Thực hiện hữu với tùy chỉnh UISearchBar i làm trong MyUISearchController:

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

@synthesize searchBar=_searchBar; 

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

Nhưng UISearchController sử dụng mặc định searchBar anyway ..

Cập nhật

Trong h file:

@interface MySearchController : UISearchController <UITableViewDelegate, UITableViewDataSource> 

Trong m file:

@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

là ẩn nút Hủy bỏ lý do duy nhất cho lớp con 'searchBar'? –

+0

@WarifAkhandRishi Có – Gikas

+0

@Gikas Vui lòng đăng một số mã để có hiểu biết tốt hơn. Rất khó để xác định vấn đề khác. Giải pháp cho vấn đề này là ẩn nút hủy trong viewsWillLayoutSubviews của bộ điều khiển xem –

Trả lời

3

UISearchController 's là một tài sản tính, bạn phải lưu trữ Searchbar tùy chỉnh của bạn và gửi lại khi gọi searchBar.

Dưới đây là một số mã trong Swift:

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

Tôi đã thấy điều này giải pháp trong http://stackoverflow.com/questions/8496310/hide-uisearchbar-cancel-button Nhưng điều này không hoạt động .. – Gikas

+0

@Gikas nó đang làm việc trong dự án demo của tôi. –

+0

Tôi đã cập nhật câu hỏi – Gikas

1

Để tùy chỉnh thuộc tính Searchbar bắt đầu quá trình chỉnh bởi subclassing đầu tiên tìm kiếm thanh (UISearchBar). Sau đó, sử dụng thanh tìm kiếm tùy chỉnh này trong lớp con của bộ điều khiển tìm kiếm và cuối cùng sử dụng cả hai trong lớp trong lớp ViewController của bạn.

Tham khảo hướng dẫn này: UISearchController Customization

0

Xem này một hướng dẫn cho tùy chỉnh UISearchBar trong Swift:

Customise UISearchBar

Objective - C: Thêm mã trong viewDidLoad. Bạn đã tuyên bố đối tượng của UISearchBar

[searchBar setShowsCancelButton:NO]; 

Swift: trong viewDidLoad

Searchbar
searchBar.showsCancelButton = false 

Image

Các vấn đề liên quan