2016-10-04 71 views
0

我有一個UITextField必須有底部邊框。由於自動版式使用我的代碼是這樣的:Swift UITextField邊框底部和陰影問題

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 

一個排除另一個。如何解決?`

回答

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

+0

所有的功能?我的問題是,需要的影子,只有當我點擊文本框 –

+0

,你可以把陰影部分裏面的另一個功能 – Rob

+0

嗯......不工作 –