UISearchBarSearchField 背景视图颜色

2024-01-26

enter image description hereI am trying to change the background color of the search bar text field and i have searched and tried lots of solutions but those are not working.

所以,请任何人都可以告诉我确切的解决方案。我们如何更改搜索栏文本字段的背景颜色?

/// used to prepare searchController inside navigation.
private func prepareNavigationSearchControllerSetup() {
    self.title = AppConstant.kContacts
    let search = UISearchController(searchResultsController: nil)
    search.searchResultsUpdater = self
    search.searchBar.cornerRadius = 10.0
    search.searchBar.textField?.backgroundColor = .red
    search.searchBar.textField?.tintColor = .yellow
    self.navigationItem.searchController = search
    self.navigationItem.hidesSearchBarWhenScrolling = false
}

extension UISearchBar {

    var textField: UITextField? {
        let subViews = subviews.flatMap { $0.subviews }
        return (subViews.filter { $0 is UITextField }).first as? UITextField
    }
}

经过更多搜索后,我找到了对我有用的正确答案。

if #available(iOS 11.0, *) {
        if let textfield = search.searchBar.value(forKey: "searchField") as? UITextField {
            textfield.textColor = UIColor.blue

            if let backgroundview = textfield.subviews.first {
                backgroundview.backgroundColor = UIColor.white
                backgroundview.layer.cornerRadius = 10;
                backgroundview.clipsToBounds = true;
            }
        }

  }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UISearchBarSearchField 背景视图颜色 的相关文章

随机推荐

  • 找到具有相同索引节点的所有文件的最快方法是什么?

    我知道的唯一方法是 find home xdev samefile file1 但确实很慢 我想找到一个类似的工具locate 当你有很多文件时 真正的问题就出现了 我认为操作是 O n 没有来自的映射inode命名 唯一的方法是遍历整个文
  • 如何解决 Eclipse SVN 中的冲突

    在使用 Eclipse 进行编码并使用 SVN 作为团队存储库时 我在 Eclipse 中遇到冲突 假设我有冲突并且有 3 个自动生成的文件 解决冲突后 我找不到如何提交冲突解决方案并自动删除生成的文件的命令 在Eclipse之外的SVN中
  • 在 PHP 中附加 XML 节点

    大家好 有人可以帮我用 PHP 修改下面的 XML 文档吗 这个问题的主要原因是我想附加一个虚拟 XML 节点 其标签名称为Service到活动 gt gt 正文 gt gt 服务 gt gt 服务安装 下面是XML文档
  • 在单元测试中提取哈希种子

    我需要获取 python 用于复制的随机哈希种子failing单元测试 If Python哈希种子 https docs python org 3 using cmdline html envvar PYTHONHASHSEED被设置为非零
  • TS 2540:无法分配给样式,因为它是只读属性

    我正在创建一个textarea我的 TSX 标记中的元素 以及Vim 中的 TS 类型检查器 https github com neoclide coc nvim抱怨2540 Cannot assign to style because i
  • 仅第一个媒体查询工作

    使用媒体查询时 似乎只有第一组有效 我不知道为什么 如果我交换它们 那么仍然只有第一个有效 我尝试过从最大宽度更改为最小宽度 我尝试删除 and 条件 我尝试删除所有 Javascript 我尝试删除所有 CSS 只保留带有 body di
  • PowerShell Unicode 字符意外转换

    我有一个程序 它使用一些哈希表来解析信息 我遇到了一些关于外国字符的奇怪问题 下面是准确的表述 Props P1 Norte Americano e Ingl s Expressions E1 Props P1 Expressions E1
  • 什么是 Linux 本地定时器中断?

    一切都在标题中 欢迎任何优秀文档的链接 本地定时器中断是在 APIC 上实现的定时器 它仅中断特定的 CPU 而不是引发可由任何 CPU 处理的中断 Bovet 和 Cesati 的 Understanding the Linux Kern
  • 在 C# 中将 string.Empty 转换为(通用)T?

    我有一个实用方法 它从旧的返回强类型值 INI配置类型文件 带有签名 internal static T GetIniSetting
  • 使用 Google 登录,仅允许组织域帐户

    我正在尝试创建一个只能通过 Google 登录才能访问且只能使用 GSuite 组织域 的 Web 应用程序 我希望这样做 而不需要事先初始化允许哪些域 这意味着它允许在 Google 下注册的所有电子邮件地址 但不允许 gmail com
  • 发布到 azure 后,与 MVC4 捆绑不起作用

    您好 我正在尝试为我的应用程序捆绑我的脚本 我的调试正在工作 如果我使用 Web debug 发布 一切都会正常 但是当我使用 Web releases 发布时 我的脚本不会加载 一切都在本地运行 只有当我从 VS2012 发布到 Azur
  • Swift 3 - Firebase 推送通知,我该怎么办?

    我确实喜欢下面的 Swift 2 但它在 Swift 3 中不起作用 我该如何提供这个 如果有人解释一下那就太好了 didFinishLaunchingWithOptions let notificationSettings UIUserN
  • 如何使用ANTLR生成的语法文件?

    我认为这是一个愚蠢的问题 但我刚刚开始使用 ANTLR 我将他们教程中的 SimpleCalc 语法放在一起 并以 C 作为目标语言生成它 我得到了 SimpleCalcParser c h 和 SimpleCalcLexer c h 作为
  • 正则表达式:懒惰更糟糕吗?

    我一直这样写正则表达式 a href target blank lt a 但我刚刚了解到这个懒惰的事 http www regular expressions info examples html我可以这样写 a href target b
  • Python无法访问字典

    Getting TypeError string indices must be integers当尝试访问 python 中的字典时 我尝试过使用json loads r2 这产生了 TypeError the JSON object m
  • 如何使用getCropAndSetWallpaperIntent?

    我尝试使用 getCropAndSetWallpaperIntent 方法 但出现错误 这是我的代码 Uri uri Uri parse content getFilesDir image path ContentResolver cont
  • Angular 8 嵌套拖放

    不幸的是我找不到常见问题的解决方案 这是代码的链接https stackblitz com edit angular ygwaxs https stackblitz com edit angular ygwaxs 我有这样的场景 从服务器我
  • 简单的 Javascript 碰撞检测?

    我正在尝试使用 jquery javascript html 和 css 制作一个简单的游戏 我一直陷入碰撞检测 code var map 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
  • 在 Sequel 中使用 UTC?

    我不想将时间存储在我当地的时区 但续集让我变得非常困难 我可以在将它们放在那里之前将它们设置为 UTC 有点痛苦 但是当我将它们取出时 它会假设它们是本地日期 然后它们都是未来的 8 小时 这是还没有实施的事情吗 如果是这样 有什么解决方法
  • UISearchBarSearchField 背景视图颜色

    I am trying to change the background color of the search bar text field and i have searched and tried lots of solutions