gpt4 book ai didi

ios - 如何在带有圆角的 UITextField 上添加阴影?

转载 作者:搜寻专家 更新时间:2023-10-31 08:16:11 26 4
gpt4 key购买 nike

我想在 UITextField 上实现圆角阴影,如下图所示:enter image description here

我的代码如下:

    override func viewDidLoad() {
super.viewDidLoad()
textField.layer.cornerRadius = textField.frame.size.height / 2
textField.layer.borderWidth = 1.0
textField.layer.borderColor = UIColor(white: 0.5, alpha: 0.3).cgColor
textField.layer.shadowOpacity = 1
textField.layer.shadowRadius = 4.0
textField.layer.shadowColor = UIColor.black.cgColor
}

但是,我觉得少了点什么……

输出: enter image description here

提前致谢!

最佳答案

尝试下面的代码在 roundRect 文本字段上实现阴影效果。

    //Basic texfield Setup 
textField.borderStyle = .none
textField.backgroundColor = UIColor.groupTableViewBackground // Use anycolor that give you a 2d look.

//To apply corner radius
textField.layer.cornerRadius = textField.frame.size.height / 2

//To apply border
textField.layer.borderWidth = 0.25
textField.layer.borderColor = UIColor.white.cgColor

//To apply Shadow
textField.layer.shadowOpacity = 1
textField.layer.shadowRadius = 3.0
textField.layer.shadowOffset = CGSize.zero // Use any CGSize
textField.layer.shadowColor = UIColor.gray.cgColor

//To apply padding
let paddingView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: textField.frame.height))
textField.leftView = paddingView
textField.leftViewMode = UITextFieldViewMode.always

注意:由于某些原因,即使在 viewWillLayoutSubviews() 中设置代码,textField.borderStyle = .none 也没有生效viewDidLayoutSubviews()。因此,我建议您通过 storyBoard textfield Attributes inspector 设置 borderStyle。

enter image description here

真实设备的输出:

enter image description here

实现投影效果:(与其他SO 帖子一样)

   textField.layer.borderColor = UIColor.black.withAlphaComponent(0.25).cgColor
textField.layer.shadowOffset = CGSize(width: 0, height: 3)
textField.layer.shadowColor = UIColor.black.cgColor //Any dark color

输出:

enter image description here

关于ios - 如何在带有圆角的 UITextField 上添加阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46562752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com