How to change UISearchBar Placeholder and image tint color?

前端 未结 12 2328
暖寄归人
暖寄归人 2020-11-29 01:24

I\'ve been trying search results for hours, but I can\'t get this figured out. Perhaps it isn\'t possible. I\'m trying to change the tint color of the placeholder text and m

相关标签:
12条回答
  • 2020-11-29 02:00

    You can change the color of the text without violating the private api rule:

    UILabel.appearanceWhenContainedInInstancesOfClasses([UITextField.self]).textColor = UIColor.whiteColor()
    
    0 讨论(0)
  • 2020-11-29 02:00

    In Swift 4, assuming your search controller is set like this:

    let searchController = UISearchController(searchResultsController: nil)
    

    then set placeholder text as follows:

    searchController.searchBar.placeholder = "Here is my custom text"
    
    0 讨论(0)
  • 2020-11-29 02:08

    I made a Swift 4.1 search bar extension:

    import Foundation
    import UIKit
    extension UISearchBar{
        func setTextField(placeHolderColor:UIColor = .gray,placeHolder:String = "Search Something",textColor:UIColor = .white,backgroundColor:UIColor = .black,
                          placeHolderFont:UIFont = UIFont.systemFont(ofSize: 12.0),
                          textFont:UIFont =  UIFont.systemFont(ofSize: 12.0) ){
            for item in self.subviews{
                for mainView in (item as UIView).subviews{
                    mainView.backgroundColor = backgroundColor
                    if mainView is UITextField{
                        let textField = mainView as? UITextField
                        if let _textF = textField{
                            _textF.text = "success"
                            _textF.textColor = textColor
                            _textF.font      = textFont
                            _textF.attributedPlaceholder = NSMutableAttributedString.init(string: placeHolder, attributes: [NSAttributedStringKey.foregroundColor : placeHolderColor,
                                                                                                                                   NSAttributedStringKey.font : placeHolderFont])
                        }
                    }
                }
            }
        }
    }
    

    You can use this for your searchBar like this :

    controller.searchBar.setTextField(placeHolderColor: .white,
     placeHolder: "Search A Pet",
     textColor: .white,
     backgroundColor: .green,
     placeHolderFont: UIFont.systemFont(ofSize: 14.0),
     textFont: UIFont.systemFont(ofSize: 14.0))
    
    0 讨论(0)
  • 2020-11-29 02:08

    For anybody just trying to change the text and having trouble seeing the updated placeholder, it worked for me by putting it here instead of viewDidLoad.

    - (void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];
        self.searchBar.placeholder = @"My Custom Text";
    }
    
    0 讨论(0)
  • 2020-11-29 02:12

    Small update to Vasily Bodnarchuk's great answer.

    Swift 4+

    NSForegroundColorAttributeName has been changed to NSAttributedStringKey.foregroundColor

    0 讨论(0)
  • 2020-11-29 02:13

    I could not make it work properly with any of the above solutions.

    I created the following UISearchBar category which works properly on iOS 8.4 and 10.3:

    UISearchBar+PlaceholderColor.h

    #import <UIKit/UIKit.h>
    
    @interface UISearchBar (PlaceholderColor)
    
    - (void)setPlaceholderColor:(UIColor *)placeholderColor;
    
    @end
    

    UISearchBar+PlaceholderColor.m

    #import "UISearchBar+PlaceholderColor.h"
    
    @implementation UISearchBar (PlaceholderColor)
    
    - (void)setPlaceholderColor:(UIColor *)placeholderColor {
        UILabel *labelView = [self searchBarTextFieldLabelFromView:self];
        [labelView setTextColor:placeholderColor];
    }
    
    - (UILabel *)searchBarTextFieldLabelFromView:(UIView *)view {
        for (UIView *v in [view subviews]) {
            if ([v isKindOfClass:[UILabel class]]) {
                return (UILabel *)v;
            }
    
            UIView *labelView = [self searchBarTextFieldLabelFromView:v];
            if (labelView) {
                return (UILabel *)labelView;
            }
        }
    
        return nil;
    }
    
    @end
    

    USAGE:

    [mySearchBar setPlaceholderColor:[UIColor redColor]];
    

    IMPORTANT NOTE:

    Make sure that you call setPlaceholderColor: AFTER your UISearchBar has been added to the view and has created its view hierarchy.

    If you open your search bar programmatically, call it AFTER your becomeFirstResponder call, as such:

    [mySearchBar becomeFirstResponder];
    [searchBar setPlaceholderColor:[UIColor redColor]];
    

    Otherwise, if you are leveraging UISearchBarDelegate:

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
        [searchBar setPlaceholderColor:[UIColor redColor]];
    }
    
    0 讨论(0)
提交回复
热议问题