如何在iOS 13中更改UISearchBar中PlaceHolder的文本颜色?

6

寻求更新的(iOS 13)答案后,我没有找到解决这个简单问题的方案:

如何更改 UISearchBar 中占位符的textColor

我的应用程序不处理Light/Dark模式。 我不希望系统更改我的UIPlaceHolder文本颜色。 我希望它始终为白色


    if #available(iOS 13.0, *) {
        let attrString = NSMutableAttributedString(string: "My PlaceHolder")
        attrString.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], range: NSRange(location: 0, length: attrString.length))
        searchController.searchBar.searchTextField.attributedPlaceholder = attrString
    }

我本以为这段代码能够正常工作。我认为新属性searchTextField会使自定义UISearchBar更加容易。

编辑:

这段代码在viewDidAppear方法中有点用处:

if #available(iOS 13.0, *) {
     searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "My PlaceHolder", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.80)])
 }

问题是当你向上或向下滚动时,颜色会改变。

嗨。也许这个主题可以帮到你 https://dev59.com/6V4b5IYBdhLWcg3w9Fqr。答案在这里 https://dev59.com/6V4b5IYBdhLWcg3w9Fqr#28499827。 - Владислав Шматок
1
我已经阅读过了。只有一个答案提到了iOS 13,而且它只是一个扩展来访问searchTextField属性。 - Funnycuni
3个回答

3
正如您先前所提到的,您的代码仅在 viewDidAppear 中运行,这会使占位符从默认灰色颜色闪烁到首选颜色。
然而,在视图实际出现之前似乎有一个时间(我没能确定具体是何时),可以在此期间更改占位符。
我想这可能与iOS如何处理浅/深模式和/或iOS错误有关。
我提出的解决方案是解决问题的方法:
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if #available(iOS 13.0, *) {
        let placeholder = NSAttributedString(string: "Search",
                                             attributes: [
                                                .foregroundColor: UIColor.white
        ])
        let searchTextField = searchBar.searchTextField

        // Key workaround to be able to set attributedPlaceholder
        DispatchQueue.global().async {
            DispatchQueue.main.async {
                searchTextField.attributedPlaceholder = placeholder
            }
        }
    }
}

这种方法似乎没有性能或其他方面的弊端。

如果有人提出更好的方法,请随时合作。


这个。这就是我一直在寻找的答案。非常感谢你。 - Manu Mateos
1
我相信你可以摆脱那个for循环,DispatchQueue.main会解决这个问题。 - Shahriyar

0

您可以使用UITextFieldappearance属性进行设置。请检查以下代码行:

 UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

1
问题是关于占位符文本的颜色,而不是文本本身的颜色。 - gklka

0

我猜测它不起作用的原因可能是你得到了searchController.searchBar.searchTextField = nil

searchTextFieldiOS 13的新属性,如果你只支持>= iOS 13,那么可以这样做。如果你想适应iOS 13之前的版本,你可以遍历UISearchBar来查找UISearchBarTextField并直接设置找到的UISearchBarTextField

我查找到的代码可能像这样,我把它放在了UIViewCategory中。

- (UIView *)findChildViewClass:(Class)childViewClass fromView:(UIView *)fromView{
    for (UIView *subView in fromView.subviews) {
        if ([subView isMemberOfClass:childViewClass]) {
            return subView;
        }
        UIView *finalView = [self findChildViewClass:childViewClass fromView:subView];
        if (finalView) {
            return finalView;
        }else{
            continue;
        }
    }
    return nil;
}

当使用时,您可以自定义searchTextField属性继承自UITextField以保存找到的值。

_searchTextField = (UITextField *)[searchBar findChildViewClass:NSClassFromString(@"UISearchBarTextField") fromView: searchBar];

这是我现在使用的,它能够正常工作,希望对你有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接