【问题标题】:How to override "searchBar" property in subclass of UISearchController?如何覆盖 UISearchController 子类中的“searchBar”属性?
【发布时间】:2016-09-02 09:30:46
【问题描述】:

我有这样的问题:Hide UISearchBar Cancel Button

我将UISearchControllerUISearchBar 子类化,并覆盖UISearchBar 中的方法layoutSubviews

如何在我的自定义UISearchController 中用自定义UISearchBar 覆盖searchBar 属性?

更新 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];
}

【问题讨论】:

  • 隐藏取消按钮是子类searchBar的唯一原因吗?
  • @WarifAkhandRishi 是的
  • @Gikas 请发布一些代码以便更好地理解。否则很难识别问题。解决此问题的方法是在视图控制器的viewsWillLayoutSubviews中隐藏取消按钮
  • @AnmolKukreja 更新

标签: ios objective-c properties uisearchbar uisearchcontroller


【解决方案1】:

UISearchController 的 searchBar 是一个计算属性,您必须存储自定义的 searchBar 并在调用 searchBar 时返回它。

以下是 Swift 中的一些代码:

var mySearchBar = MySearchBar()

override var searchBar: UISearchBar {
    get{
        return mySearchBar;
    }
}

【讨论】:

    【解决方案2】:

    为了自定义 searchBar 属性,首先通过子类化搜索栏 (UISearchBar) 来开始自定义过程。然后,在搜索控制器的子类中使用这个自定义搜索栏,最后在您的 ViewController 类中使用它们。

    参考本教程: UISearchController Customization

    【讨论】:

      【解决方案3】:
      import UIKit
      
      class MySearchController: UISearchController {
      
          override func viewWillLayoutSubviews() {
              super.viewWillLayoutSubviews()
              searchBar.showsCancelButton = false
          }
      }
      

      【讨论】:

      【解决方案4】:

      请参阅此教程以在 Swift 中自定义 UISearchBar:

      Customise UISearchBar

      目标 - C : 在 ViewDidLoad 中添加代码。你已经声明了UISearchBar的对象

      [searchBar setShowsCancelButton:NO]; 
      

      Swift:在 ViewDidLoad 中

      searchBar.showsCancelButton = false
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 1970-01-01
        • 1970-01-01
        • 2016-11-06
        相关资源
        最近更新 更多