【问题标题】:Change the search placeholder by code通过代码更改搜索占位符
【发布时间】:2017-02-10 09:17:46
【问题描述】:

我正在尝试通过在 viewDidLoad 中使用此代码,在表格视图中将搜索占位符更改为“搜索设备”:

  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

我确定代码中一切正常,但是为什么占位符没有改变?!

【问题讨论】:

    标签: ios swift tableview uisearchbar placeholder


    【解决方案1】:

    您的代码运行良好,请尝试将 sn-p 删除到您的 viewDidLoad 中。

    【讨论】:

    • 我提到您应该将所有代码放在 viewDidAppear 而不是 viewDidLoad 中。
    • 您是否将占位符设置在其他位置?也许故事板也有?
    • 很难说为什么它不起作用..你能分享你的代码,我会尽力帮助你。
    猜你喜欢
    • 2016-09-17
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 2022-12-06
    • 2014-12-14
    • 2019-06-20
    • 1970-01-01
    相关资源
    最近更新 更多