通过代码更改搜索占位符

Change the search placeholder by code

我正在尝试通过在 viewDidLoad:

中使用以下代码,将 table 视图中的搜索占位符更改为 "Search for a device"
  let placeholderAttributes: [String : AnyObject] = [NSForegroundColorAttributeName: UIColor.blue, NSFontAttributeName: UIFont.systemFont(ofSize: UIFont.systemFontSize)]
        let attributedPlaceholder: NSAttributedString = NSAttributedString(string: "Search for a Device", attributes: placeholderAttributes)
        let textFieldPlaceHolder = searchBar.value(forKey: "searchField") as? UITextField
        textFieldPlaceHolder?.attributedPlaceholder = attributedPlaceholder

我确信代码中的一切都没有问题,但为什么占位符没有改变?!

您的代码运行良好,请尝试将代码段移除到您的 viewDidLoad 中。