1

This is my code:

@IBOutlet var txtFirstName: MDCTextField!
var txtFirstNameController: MDCTextInputControllerOutlined?
override func viewDidLoad() {
txtFirstNameController = MDCTextInputControllerOutlined(textInput: txtFirstName)
}

I want to add shadow to the text field but I couldn't find out how, I am using MaterialComponents I have mentioned all the ways that I have tested and their results here:

1st way:

class ShadowLayer : MDCTextField{
override class var layerClass: AnyClass {
    return MDCShadowLayer.self
}

var shadowLayer: MDCShadowLayer {
    return self.layer as! MDCShadowLayer
}

func setDefaultElevation() {
    self.shadowLayer.elevation = .cardResting
}
}

2nd way:

extension MDCTextField {

func elevate(elevation: Double) {
    self.backgroundColor = UIColor.white
    self.layer.masksToBounds = false
    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowOffset = CGSize(width: 0, height: elevation)
    self.layer.shadowRadius = CGFloat(elevation)
    self.layer.shadowOpacity = 1

}
}

choose country -> 1st way, choose city-> 2nd way

choose country -> 1st way, choose city-> 2nd way

what I want is the pic below:

enter image description here

Hamish
  • 1,685
  • 22
  • 37
  • did you try to set txtFirstName.layer.masksToBounds = false; txtFirstName.layer.shadowRadius = 3.0; txtFirstName.layer.shadowColor = UIColor.black.cgColor; txtFirstName.layer.shadowOffset = CGSize(width: 2.0, height: 4.0); txtFirstName.layer.shadowOpacity = 1.0; – Fkzm Feb 17 '19 at 13:26
  • yes but it does not work... – Hamish Feb 17 '19 at 13:43
  • provide more related code. what have you tried? What works? What doesn't? Any picture of expected results? Any picture of current results? – Gustavo Vollbrecht Feb 17 '19 at 14:13
  • I add the ways and the pics @GustavoVollbrecht – Hamish Feb 17 '19 at 14:30
  • im having the same result, but instead im trying to add a white background color – João Serra Apr 11 '19 at 16:40

1 Answers1

0

Try setting self.clipsToBounds = true before setting self.layer.masksToBounds = false

Your elevate function should look like this:

func elevate(elevation: Double) {
    self.backgroundColor = UIColor.white
    self.clipsToBounds = true
    self.layer.masksToBounds = false
    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowOffset = CGSize(width: 0, height: elevation)
    self.layer.shadowRadius = CGFloat(elevation)
    self.layer.shadowOpacity = 1
}
Gustavo Vollbrecht
  • 3,188
  • 2
  • 19
  • 37