如何在 UISearchController 的子类中覆盖 "searchBar" 属性?

12

我有这样的问题:如何隐藏UISearchBar的取消按钮?

我已经对UISearchControllerUISearchBar进行了子类化,并在UISearchBar中覆盖了layoutSubviews方法。

如何使用自定义的UISearchBar重写我的自定义UISearchController中的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 的唯一原因吗? - Warif Akhand Rishi
@WarifAkhandRishi 是的 - Gikas
@Gikas,请贴一些代码以便更好地理解。否则很难确定问题所在。解决此问题的方法是在视图控制器的viewsWillLayoutSubviews中隐藏取消按钮。 - Anmol Kukreja
@AnmolKukreja 已更新 - Gikas
4个回答

6

UISearchControllersearchBar 是一个计算属性,你需要先存储你的自定义搜索栏,然后在调用 searchBar 时返回它。

下面是一些 Swift 代码:

var mySearchBar = MySearchBar()

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

1
为了定制searchBar属性,首先通过子类化搜索栏(UISearchBar)开始定制过程。然后,在搜索控制器的子类中使用此自定义搜索栏,并最终在您的ViewController类中同时使用它们两个。
参考此教程: UISearchController Customization

0
import UIKit

class MySearchController: UISearchController {

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

我在https://dev59.com/smoy5IYBdhLWcg3wg-Ym中看到了这个解决方案,但是它并不起作用。 - Gikas

0

查看这个关于在Swift中自定义UISearchBar的教程:

自定义UISearchBar

Objective-C:在ViewDidLoad中添加代码。您已经声明了UISearchBar对象。

[searchBar setShowsCancelButton:NO]; 

Swift:在ViewDidLoad中

searchBar.showsCancelButton = false

Image


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接