简体   繁体   English

Swift - 如何为 UISearchBar 提供阴影但 * 不是 * 取消按钮?

[英]Swift - How to give shadow to UISearchBar but *not* its cancel button?

I have a search bar that I have applied a drop shadow to, and as you can see here , the cancel button casts its own shadow too.我有一个应用了阴影的搜索栏,正如你在这里看到的,取消按钮也投射了自己的阴影。 I would like for the shadow to be limited to the search text box.我希望阴影仅限于搜索文本框。 Here is what I'm starting with:这是我的开始:

    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

What I've tried:我试过的:

  1. I tried altering the cancel button's appearance via UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]) , but it turns out that this object's shadow is a separate one.我尝试通过UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])改变取消按钮的外观,但事实证明这个对象的阴影是一个单独的。 Notice the subtle difference around the letters after I applied another shadow by modifying the appearance shadow properties.通过修改外观阴影属性应用另一个阴影后,请注意字母周围的细微差别

  2. Giving the button a white image background didn't work, which I did by accessing searchBar.value(forKey: "cancelButton") as! UIButton给按钮一个白色的图像背景不起作用,我通过访问searchBar.value(forKey: "cancelButton") as! UIButton searchBar.value(forKey: "cancelButton") as! UIButton . searchBar.value(forKey: "cancelButton") as! UIButton The shadow is applied around the whole image and looks even worse.阴影应用于整个图像,看起来更糟。

  3. Adjusting the bounds of the search bar layer unfortunately did not work either ( searchBar.layer.frame = CGRect(x: searchBar.frame.minX, y: searchBar.frame.minY, width: searchBar.frame.width - cancelButton.frame.width, height: searchBar.frame.height) ).不幸的是,调整搜索栏层的边界也不起作用( searchBar.layer.frame = CGRect(x: searchBar.frame.minX, y: searchBar.frame.minY, width: searchBar.frame.width - cancelButton.frame.width, height: searchBar.frame.height) )。 I gave the search bar layer a border to observe this, and the cancel button shadow persists .我给了搜索栏层一个边框来观察这一点,取消按钮阴影仍然存在

  4. I also tried unlinking the cancel button from the superview, but was only able to make it disappear entirely.我还尝试从超级视图中取消取消按钮的链接,但只能让它完全消失。

Is there any way around this?有没有办法解决? I'm on Swift 5 and iOS 14.我使用的是 Swift 5 和 iOS 14。

As ibrahimyilmaz suggested, adding the shadow to the UITextField instead of UISearchBar got the job done.正如 ibrahimyilmaz 建议的那样,将阴影添加到 UITextField 而不是 UISearchBar 就完成了工作。 Just had to modify my height and layout anchor constraints to handle clipping.只需要修改我的高度和布局锚点约束来处理剪裁。

I followed this example by user Joshpy in order to access the text field.我按照用户 Joshpy 的这个例子来访问文本字段。 For iOS >= 13, making the shadow is as simple as:对于 iOS >= 13,制作阴影很简单:

    searchBar.searchTextField.layer.shadowColor = UIColor.black.cgColor
    searchBar.searchTextField.layer.shadowOpacity = 0.25
    searchBar.searchTextField.layer.shadowOffset = CGSize(width: 2, height: 2)
    searchBar.searchTextField.layer.shadowRadius = 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM