UISearchBar change placeholder color

前端 未结 17 2413
南旧
南旧 2020-12-04 15:16

Has anyone any idea or code sample on how can I change the text color of the placeholder text of a UISearchBar?

相关标签:
17条回答
  • 2020-12-04 15:57

    iOS 13

    Use a custom search bar.

    This also works when part of a UISearchController inside a UINavigationItem (with hidesSearchBarWhenScrolling = true).

    We want to apply our changes immediately after UIAppearance proxies are being applied since those are the most likely root cause:

    class MySearchBar : UISearchBar {
        // Appearance proxies are applied when a view is added to a view hierarchy, so apply your tweaks after that:
        override func didMoveToSuperview() {
            super.didMoveToSuperview() // important! - system colors will not apply correctly on ios 11-12 without this
    
            let placeholderColor = UIColor.white.withAlphaComponent(0.75)
            let placeholderAttributes = [NSAttributedString.Key.foregroundColor : placeholderColor]
            let attributedPlaceholder = NSAttributedString(string: "My custom placeholder", attributes: placeholderAttributes)
            self.searchTextField.attributedPlaceholder = attributedPlaceholder
            
            // Make the magnifying glass the same color
            (self.searchTextField.leftView as? UIImageView)?.tintColor = placeholderColor
        }
    }
    
    // Override `searchBar` as per the documentation
    private class MySearchController : UISearchController {
        private lazy var customSearchBar = MySearchBar()
        override var searchBar: UISearchBar { customSearchBar }
    }
    

    That took quite some time to get working properly...

    0 讨论(0)
  • 2020-12-04 15:59

    After surveyed a couple of answers, I come out this, hope its help

    for (UIView *subview in searchBar.subviews) {
        for (UIView *sv in subview.subviews) {
            if ([NSStringFromClass([sv class]) isEqualToString:@"UISearchBarTextField"]) {
    
                if ([sv respondsToSelector:@selector(setAttributedPlaceholder:)]) {
                    ((UITextField *)sv).attributedPlaceholder = [[NSAttributedString alloc] initWithString:searchBar.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
                }
                break;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 16:03

    Found the answer from Change UITextField's placeholder text color programmatically

    // Get the instance of the UITextField of the search bar
    UITextField *searchField = [searchBar valueForKey:@"_searchField"];
    
    // Change search bar text color
    searchField.textColor = [UIColor redColor];
    
    // Change the search bar placeholder text color
    [searchField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];
    
    0 讨论(0)
  • 2020-12-04 16:04

    for iOS5+ use the appearance proxy

    [[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
    
    0 讨论(0)
  • 2020-12-04 16:04

    This solution works on Xcode 8.2.1. with Swift 3.0. :

    extension UISearchBar
    {
        func setPlaceholderTextColorTo(color: UIColor)
        {
            let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
            textFieldInsideSearchBar?.textColor = color
            let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
            textFieldInsideSearchBarLabel?.textColor = color
        }
    }
    

    Usage example:

    searchController.searchBar.setPlaceholderTextColorTo(color: mainColor)
    
    0 讨论(0)
  • 2020-12-04 16:04

    This is an old question, but for anyone stumbling on it nowadays, you can change the search icon on iOS 8.x - 10.3 using the following:

    [_searchBar setImage:[UIImage imageNamed:@"your-image-name"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    

    Regarding the placeholder text color, you may check my other answer, which uses a Category, here: UISearchBar change placeholder color

    0 讨论(0)
提交回复
热议问题