Swift UITextField边框底部和阴影问题

时间:2016-10-04 14:59:25

标签: ios swift uitextfield

我有一个必须有底部边框的UITextField。由于autolayout使用我的代码是这样的:

override func viewDidLayoutSubviews() {
    let border = CALayer()
    let width = CGFloat(0.5)
    border.borderColor = UIColor.darkGray.withAlphaComponent(0.3).cgColor
    border.frame = CGRect(x: 0, y: txtUsername.frame.size.height - width, width:  txtUsername.frame.size.width, height: txtUsername.frame.size.height)

    border.borderWidth = width
    txtUsername.layer.addSublayer(border)
    txtUsername.layer.masksToBounds = true
}

然而,这排除了这样一个事实:当我触摸UITextField时,这应该会给我一个阴影。代码是这样的:

    //ombra txtField
   self.view.addSubview(txtUsername)
    txtUsername.layer.shadowOffset = CGSize(width: 5, height: 10)
    txtUsername.layer.shadowOpacity = 0.3
    txtUsername.layer.shadowRadius = 10
    txtUsername.layer.masksToBounds = false;
    txtUsername.clipsToBounds = false
那个人排除了另一个人。我该如何解决?“

1 个答案:

答案 0 :(得分:2)

您可以使用CAShapeLayerShadow

func border(){
let border = CALayer()
    let width = CGFloat(0.5)
    border.borderColor = UIColor.darkGray.withAlphaComponent(1).cgColor
    border.frame = CGRect(x: 0, y: CustomerTextBox.frame.size.height - width, width:  CustomerTextBox.frame.size.width, height: CustomerTextBox.frame.size.height)

    border.borderWidth = width
    CustomerTextBox.layer.addSublayer(border)
    CustomerTextBox.layer.masksToBounds = true

  }


   func shadow(){
    let Shape = CAShapeLayer()
     let myPath = UIBezierPath(ovalIn: CustomerTextBox.frame)

    Shape.shadowPath = myPath.cgPath
    Shape.shadowColor = UIColor.lightGray.cgColor
    Shape.shadowOffset = CGSize(width: 5, height: 3)
    Shape.shadowRadius  = 5
    Shape.shadowOpacity = 0.8


    view.layer.insertSublayer(Shape, at: 0)

   }

enter image description here

相关问题