iOS 11 customise search bar in navigation bar

前端 未结 7 1154
自闭症患者
自闭症患者 2020-11-28 19:26

I want to change the color of the text and icon in the iOS 11 searchbar when it is embedded in the navigation bar. So placeholder text, search text and search icon.

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 20:06

    my two cents for Swift 4.x, lightly cleaned up.

    Add in controller or App Delegate:

    appearance.backgroundColor = .green
    let myFont = UIFont.italicSystemFont(ofSize: 12)
    let attribs = [
        NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue): myFont,
        NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.red
    ]
    
    appearance.defaultTextAttributes =  attribs
    

    in controller:

    self.searchBar.barTintColor = .blue
    

    You will get Blue background, green search bar background, red italic font:

提交回复
热议问题