IOS rounded UITextField with shadow

I would like to add a shadow effect for my UITextField, I am currently achieving this: enter image description here
As you can see, the shadow is not rounded at the corners. My code is:

mNickname.layer.borderWidth = 1 mNickname.layer.borderColor = UIColor.whiteColor().CGColor mNickname.layer.cornerRadius = 3 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 
+7
source share
6 answers

Ok, I found a problem. Turns out I used a rectangle:

enter image description here

The correct one should be:

enter image description here

And the code:

 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:

enter image description here

+18
source

Try changing the shadowOpacity to 0.5. Also, can you send the full customization of this text box?

+1
source

try this, the code is in object C, but the same for quick

 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; 
+1
source

Have you tried this mNickname.layer.masksToBounds = true;

0
source

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; } } 
0
source
 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) 
0
source

Source: https://habr.com/ru/post/1242748/


All Articles