如何在 UISearchController 的子类中覆盖 "searchBar" 属性?
How to override "searchBar" property in subclass of UISearchController?
我遇到这样的问题:Hide UISearchBar Cancel Button
我对 UISearchController
和 UISearchBar
进行了子类化,并覆盖了 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];
}
import UIKit
class MySearchController: UISearchController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
searchBar.showsCancelButton = false
}
}
为了自定义搜索栏 属性 通过首先子class搜索栏 (UISearchBar) 开始自定义过程。然后,在搜索控制器的 subclass 中使用这个自定义搜索栏,最后在 ViewController class.
中使用它们
请参阅此教程以自定义 Swift 中的 UISearchBar:
Objective - C : 在ViewDidLoad中添加代码。您已经声明了 UISearchBar
的对象
[searchBar setShowsCancelButton:NO];
Swift : 在 ViewDidLoad
searchBar.showsCancelButton = false
UISearchController
的 searchBar 是一个计算 属性,你必须存储你的自定义 searchBar 并且 return 它在调用 searchBar
.
时
下面是 Swift 中的一些代码:
var mySearchBar = MySearchBar()
override var searchBar: UISearchBar {
get{
return mySearchBar;
}
}
我遇到这样的问题:Hide UISearchBar Cancel Button
我对 UISearchController
和 UISearchBar
进行了子类化,并覆盖了 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];
}
import UIKit
class MySearchController: UISearchController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
searchBar.showsCancelButton = false
}
}
为了自定义搜索栏 属性 通过首先子class搜索栏 (UISearchBar) 开始自定义过程。然后,在搜索控制器的 subclass 中使用这个自定义搜索栏,最后在 ViewController class.
中使用它们请参阅此教程以自定义 Swift 中的 UISearchBar:
Objective - C : 在ViewDidLoad中添加代码。您已经声明了 UISearchBar
[searchBar setShowsCancelButton:NO];
Swift : 在 ViewDidLoad
searchBar.showsCancelButton = false
UISearchController
的 searchBar 是一个计算 属性,你必须存储你的自定义 searchBar 并且 return 它在调用 searchBar
.
下面是 Swift 中的一些代码:
var mySearchBar = MySearchBar()
override var searchBar: UISearchBar {
get{
return mySearchBar;
}
}