I would like to add a shadow effect to my UITextField currently what I\'m achieving is this:
As you can see the shadow is not rounded in the corners. My code:
extension UITextField {
func addShadowToTextField(color: UIColor = UIColor.gray, cornerRadius: CGFloat) {
self.backgroundColor = UIColor.white
self.layer.masksToBounds = false
self.layer.shadowColor = color.cgColor
self.layer.shadowOffset = CGSize(width: 0, height: 0)
self.layer.shadowOpacity = 1.0
self.backgroundColor = .white
self.layer.cornerRadius = cornerRadius
}
}
Add Extension and Usage:
dateTxtFld.addShadowToTextField(cornerRadius: 3)
dateTxtFld.addShadowToTextField(color: UIColor.blackColor, cornerRadius: 3)
Ok, I found the problem. Turns out that I was using rectangle border:
The correct one should be:
And the code is:
mNickname.layer.masksToBounds = false
mNickname.layer.shadowRadius = 3.0
mNickname.layer.shadowColor = UIColor.blackColor().CGColor
mNickname.layer.shadowOffset = CGSizeMake(1.0, 1.0)
mNickname.layer.shadowOpacity = 1.0
Result:
Try to change shadowOpacity to 0.5 Also, could you send full customisation of this textField?
do you tried this mNickname.layer.masksToBounds = true;
try this, code is in objective C, but same for swift
self.textField.layer.shadowColor = [[UIColor blackColor] CGColor];
self.textField.layer.shadowOffset = CGSizeMake(0.0, 1.0);
self.textField.layer.shadowOpacity = 1;
self.textField.layer.shadowRadius = 0.0;
You can use this extension
extension ViewController {
func setShadow(_ view: UIView) {
view.layer.masksToBounds = false;
view.layer.shadowRadius = 3.0;
view.layer.shadowColor = UIColor.black.cgColor;
view.layer.shadowOffset = CGSize(width: 2.0, height: 4.0);
view.layer.shadowOpacity = 1.0;
}
}