ios - 如何使搜索栏角变圆

标签 ios swift uinavigationbar uisearchbar

我正在尝试使 searchBar 角变圆。我已经尝试了很多使用 layer.cornerRadius 的方法,maskToBound = true。我正在以编程方式在 navigationBar 中加载 searchBar。我尝试了很多关于堆栈溢出的解决方案,但没有任何效果。

func setupsearchbar()
{
    //      Setup the Search Controller
    searchcontroller.searchResultsUpdater = self as? UISearchResultsUpdating
    searchcontroller.hidesNavigationBarDuringPresentation = false
    searchcontroller.searchBar.placeholder = "Search" 
    self.navigationItem.titleView = searchcontroller.searchBar
    self.searchcontroller.searchBar.layer.cornerRadius = 30
    self.searchcontroller.searchBar.layer.masksToBounds = true
    definesPresentationContext = true
    searchcontroller.searchBar.delegate = self
}

如果有什么我能做的,请帮忙。

最佳答案

在我当前的项目中使用以下对我来说工作正常的代码。

if let textfield = searchController.searchBar.value(forKey: "searchField") as? UITextField {

    textfield.textColor = UIColor.blue

    if let backgroundview = textfield.subviews.first {

        // Background color
        backgroundview.backgroundColor = UIColor.white

        // Rounded corner
        backgroundview.layer.cornerRadius = 10;
        backgroundview.clipsToBounds = true;
    }
}

希望对您有所帮助。

关于ios - 如何使搜索栏角变圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52870677/

相关文章:

ios - UIView.transition动画它只工作一次,第二次图片消失

Swift - 栏按钮项目图像/设置大小和宽度

objective-c - Swift:如何通过 SCDynamicStoreCopyConsoleUser 获取控制台用户、UID 和 GID?

iOS Swift TabBar 状态栏文本颜色故障一半白色一半黑色

iphone - 使用[UINavigationBar外观]更改后退按钮图像

ios - 添加一个带有渐变颜色的调整大小的 UIButton 背景

iphone - 监听 TabBar View 变化

swift - 手动创建 nsbutton - 调用函数

ios - 使用 tabBar 从应用程序中的 navBar 推送 View

ios - 找不到成员(member)下标