Swift修改UISearchBar上取消按钮文本的颜色

时间:2022-06-01 12:56:44

Swift 2,3,4在语法上少有不同,以下是各个Swift版本修改取消按钮文本颜色的代码片段

Swift 4.0

let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
 UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes , for: .normal)

Swift 3.1

let cancelButtonAttributes: [String: AnyObject] = [NSForegroundColorAttributeName: UIColor.white]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)

Swfit 2

let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], forState: UIControlState.Normal)