UITextField没有边框和阴影

时间:2016-06-24 13:35:37

标签: ios swift uitextfield border shadow

我正在努力克服一件愚蠢的事情......这是我的问题。我想摆脱我的圆形灰色边框使其隐藏或透明,所以我们只能看到阴影。

以下是我的情况:

Actual Result

使用以下代码:

 private func styleTextField(textField: UITextField)
{
    textField.borderStyle = UITextBorderStyle.RoundedRect
   //textField.layer.cornerRadius = 5.0
   // textField.borderStyle = UITextBorderStyle.None
    textField.layer.borderWidth = 0.0
    textField.layer.masksToBounds = false
    textField.layer.shadowRadius = 4.0
    textField.layer.borderColor = UIColor.whiteColor().CGColor
    textField.layer.shadowColor = UIColor.grayColor().CGColor
    textField.layer.shadowOffset = CGSizeMake(0.0, 0.0)
    textField.layer.shadowOpacity = 0.4
    //textField.layer.borderColor = UIColor.clearColor().CGColor
}

但我希望得到以下结果:

Final wanted result

当然,我认为我可以实现这一目标,但将其嵌入到视图中,但它并不干净,特别是对于这类事物。

关于如何实现这一点的任何想法?或者解决这个问题?

编辑1:建议后的实际代码。如果这可以帮助。

`class SignUpViewController:UIViewController {

@IBOutlet weak var facebookButton: UIButton!
@IBOutlet weak var connectButton: UIButton!
@IBOutlet weak var passField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var nomField: UITextField!
@IBOutlet weak var prenomField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()


    emailField = self.styleTextField(emailField)
    passField = self.styleTextField(passField)
    nomField = self.styleTextField(nomField)
    prenomField = self.styleTextField(prenomField)

    self.styleButton(self.connectButton)
    self.styleButton(self.facebookButton)
}


private func styleTextField(textField: UITextField) -> UITextField
{
    textField.borderStyle = UITextBorderStyle.RoundedRect
    textField.layer.borderWidth = 2.0
    textField.layer.borderColor = UIColor.clearColor().CGColor


    textField.layer.masksToBounds = false
    textField.layer.shadowColor = UIColor.lightGrayColor().CGColor
    textField.layer.shadowOpacity = 0.5
    textField.layer.shadowRadius = 4.0
    textField.layer.shadowOffset = CGSizeMake(0.0, 1.0)

    return textField
}

}`

编辑2:我在Storyboard中创建边框时的边框类型。 Border type at creation 的问候,

HARY

3 个答案:

答案 0 :(得分:4)

更新SWIFT 4.2:

通过将所有内容集中在图层上,使其成为可行的工作:

private func styleTextField(textField: UITextField){
    textField.borderStyle = .none
    textField.layer.masksToBounds = false
    textField.layer.cornerRadius = 5.0;
    textField.layer.backgroundColor = UIColor.white.cgColor
    textField.layer.borderColor = UIColor.clear.cgColor
    textField.layer.shadowColor = UIColor.black.cgColor
    textField.layer.shadowOffset = CGSize(width: 0, height: 0)
    textField.layer.shadowOpacity = 0.2
    textField.layer.shadowRadius = 4.0
}

感谢您的帮助!

问候,

HARY

答案 1 :(得分:1)

试试这个:

textField.layer.borderColor = UIColor.clearColor().CGColor
    textField.layer.masksToBounds = false

    textField.layer.shadowColor = UIColor.blackColor().CGColor

    textField.layer.shadowOpacity = 1.0
    textField.layer.shadowRadius = 50.0

然后继续 enter image description here

答案 2 :(得分:0)

Swift 4.2代码是IBDesignable Shadow类,可轻松与情节提要一起使用并查看所有更改

@IBDesignable
class CustomTextfiled: UITextField {

override func awakeFromNib() {
    super.awakeFromNib()
}

override func layoutSubviews() {
    super.layoutSubviews()
}


@IBInspectable var borderColor: UIColor = UIColor.black {
    didSet {

        layer.borderColor = borderColor.cgColor
        borderStyle = UITextField.BorderStyle.none
        layer.masksToBounds = false
        layer.cornerRadius = 5.0;
        layer.backgroundColor = UIColor.white.cgColor
        layer.borderColor = UIColor.clear.cgColor
        layer.shadowColor = borderColor.cgColor
        layer.shadowOffset = CGSize(width: 0, height: 0)
        layer.shadowOpacity = 0.15
        layer.shadowRadius = 4.0

    }
}
相关问题