gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:05:49 26 4
gpt4 key购买 nike

我遇到这样的问题: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];
}

最佳答案

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

这里是 Swift 中的一些代码:

var mySearchBar = MySearchBar()

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

关于ios - 如何覆盖 UISearchController 子类中的 "searchBar"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39289034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com