首页 文章

iOS 11自定义导航栏中的搜索栏

提问于
浏览
33

我想在iOS 11搜索栏中嵌入导航栏时更改文本和图标的颜色 . 所以占位符文本,搜索文本和搜索图标 .

enter image description here

if #available(iOS 11.0, *) {
    navigationController?.navigationBar.prefersLargeTitles = false
    let searchController = UISearchController(searchResultsController: nil)
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false

    searchController.searchBar.placeholder = "Suchen"
    searchController.searchBar.tintColor = .white
}

正如您在图像中看到的那样,文本在深蓝色背景上呈灰色,看起来很难看 . 我希望文字和图标至少是白色的 . (改变蓝色背景色也行不通,见my other question

唯一有效的方法是改变闪烁光标的颜色和“取消”按钮,这是通过.tintColor属性完成的 .

似乎在iOS 10及以下版本中工作的解决方案在iOS 11中似乎不再起作用,因此请仅发布您在iOS 11中工作的解决方案 . 谢谢 .

也许我错过了iOS 11中关于这种“自动样式”的观点 . 任何帮助都表示赞赏 .

5 回答

  • 3

    我刚刚发现了如何设置其余部分:(在布兰登的帮助下,谢谢!)

    “取消”文字:

    searchController.searchBar.tintColor = .white
    

    搜索图标:

    searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.search, state: .normal)
    

    清晰的图标:

    searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.clear, state: .normal)
    

    搜索文字:

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
    

    感谢@Brandon的帮助!

    enter image description here

    占位符:

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
    

    enter image description here

    白色背景:

    if #available(iOS 11.0, *) {
        let sc = UISearchController(searchResultsController: nil)
        sc.delegate = self
        let scb = sc.searchBar
        scb.tintColor = UIColor.white
        scb.barTintColor = UIColor.white
    
        if let textfield = scb.value(forKey: "searchField") as? UITextField {
            textfield.textColor = UIColor.blue
            if let backgroundview = textfield.subviews.first {
    
                // Background color
                backgroundview.backgroundColor = UIColor.white
    
                // Rounded corner
                backgroundview.layer.cornerRadius = 10;
                backgroundview.clipsToBounds = true;
            }
        }
    
        if let navigationbar = self.navigationController?.navigationBar {
            navigationbar.barTintColor = UIColor.blue
        }
        navigationItem.searchController = sc
        navigationItem.hidesSearchBarWhenScrolling = false
    }
    

    enter image description here

    取自here .

  • 91

    设置搜索文本颜色

    (UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) ).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    

    设置搜索占位符颜色

    (UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) ).attributedPlaceholder = [NSForegroundColorAttributeName: UIColor.white]
    
  • 4

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

    UISearchBar.appearance().tintColor = UIColor.whiteAppDelegate .

    或者,将它们放在 [UIViewController viewDidLoad:]

  • -3

    除了Darko 's answer. In my case I need to get pure white search textfield color. Also I need a custom borderLine and cornerRadius. So if I just set background color to white, set custom corner radius and custom border line I'之外,还有类似的东西 .
    Look at this ugly grey highLight when you tap on textfield

    问题是搜索栏有一些子视图,我刚删除它们 . 这是我的代码:

    @interface YourViewController () <UISearchBarDelegate, UISearchControllerDelegate>
    // your properties
    @property (nonatomic,strong) UISearchController *searchController;
    @property (nonatomic,strong) UISearchBar *searchBar;
    @end
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    
    _searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
    _searchController.delegate = self;
    _searchController.searchBar.delegate = self;
    _searchBar = self.searchController.searchBar;
    
    if (@available(iOS 11.0, *)) {
    
        UITextField *searchTextField = [_searchBar valueForKey:@"searchField"];
    if (searchTextField != nil) {
            searchTextField.layer.cornerRadius = 4.f;
            searchTextField.layer.borderWidth = 1.f;
            searchTextField.clipsToBounds = YES;
    
            for (UIView *subView in searchTextField.subviews) {
                [subView removeFromSuperview];
            }
    }
    
    // Color for "Cancel" button
        _searchBar.tintColor = [UIColor blackColor];
    // Add searchController to navgationBar
        _navigationItem.searchController = _searchController;
    // Hide searchBar when scroll
        _navigationItem.hidesSearchBarWhenScrolling = YES;
    }
    }
    

    现在我点击时禁用了灰色突出显示 .
    Editing searchfield

    Resign first responder

  • 1

    此代码更改文本字段的背景颜色

    Swift 4

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
            //background color of text field
             UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan
    
            }
    

    编辑:青色的UISearchBar样本

    sample

相关问题