Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS rounded UITextField with shadow

Tags:

ios

swift

I would like to add a shadow effect to my UITextField currently what I'm achieving is this: enter image description here
As you can see the shadow is not rounded in the corners. My code:

        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
like image 473
Radoslav Yordanov Avatar asked Feb 11 '16 07:02

Radoslav Yordanov


2 Answers

Ok, I found the problem. Turns out that I was using rectangle border:

enter image description here

The correct one should be:

enter image description here

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:

enter image description here

like image 112
Radoslav Yordanov Avatar answered Oct 18 '22 09:10

Radoslav Yordanov


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;
like image 2
Chanaka Anuradh Caldera Avatar answered Oct 18 '22 07:10

Chanaka Anuradh Caldera