【发布时间】:2020-11-18 05:57:38
【问题描述】:
我有一个已应用投影的搜索栏,如您所见here,取消按钮也投下自己的阴影。我希望阴影仅限于搜索文本框。这是我要开始的内容:
let searchBar = UISearchBar()
searchBar.placeholder = "KAT Milkshake"
searchBar.backgroundImage = UIImage()
searchBar.barTintColor = UIColor.white
searchBar.compatibleSearchTextField.leftView?.tintColor = UIColor.gray
searchBar.compatibleSearchTextField.backgroundColor = UIColor.white
searchBar.layer.shadowColor = UIColor.black.cgColor
searchBar.layer.shadowOpacity = 0.25
searchBar.layer.shadowOffset = CGSize(width: 2, height: 2)
searchBar.layer.shadowRadius = 5
我尝试过的:
-
我尝试通过
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])更改取消按钮的外观,但事实证明该对象的阴影是单独的。在我通过修改外观阴影属性应用另一个阴影之后,请注意微妙的difference around the letters。 -
给按钮一个白色图像背景不起作用,我通过访问
searchBar.value(forKey: "cancelButton") as! UIButton做到了。阴影应用around the whole image,看起来更糟。 -
不幸的是,调整搜索栏层的边界也不起作用(
searchBar.layer.frame = CGRect(x: searchBar.frame.minX, y: searchBar.frame.minY, width: searchBar.frame.width - cancelButton.frame.width, height: searchBar.frame.height))。 我给了搜索栏层一个边框来观察这个,取消按钮阴影 persists. -
我也尝试从超级视图取消链接取消按钮,但只能让它完全消失。
有没有办法解决这个问题?我正在使用 Swift 5 和 iOS 14。
【问题讨论】:
-
你可以找到 UISearchBar 的 UITextField 并自定义它。您可以搜索有关从 searchBar 访问 textField 的扩展。
-
@ibrahimyilmaz 哇,谢谢!我觉得很傻,没有想到这一点。它几乎可以工作 - 导致顶部和底部阴影出现一些难看的剪裁。如果我知道如何在不减小阴影半径的情况下修复它,我会发布答案。
标签: ios swift uikit uisearchbar