4

i am trying to round the top and the bottom border of two UITextField

The result in ViewDidLoad()

ViewDidLoad Result

The result in ViewDidAppear

ViewDidAppear Result

The problem is ViewDidAppear makes the border changes after half a sec of loading the view which is not good in my situation ,

any one knows why it's only rounding the left corner in ViewDidLoad method ?

any suggestions appreciated ?

* Update *

viewDidLayoutSubviews is the same as ViewDidLoad

Here is the code i use to round the corners

extension UITextField {
    func roundCorners(corners:UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.CGPath
        self.layer.masksToBounds = true
        self.layer.mask = mask
    }
}

And

self.FirstTextField.roundCorners(UIRectCorner.TopRight | UIRectCorner.TopLeft, radius: 10.0)
self.SecondTextField.roundCorners(UIRectCorner.BottomLeft | UIRectCorner.BottomRight, radius: 10.0)
Amr Mohamed
  • 2,290
  • 4
  • 20
  • 39

3 Answers3

5

The one thing that i have ever expected to fix this is to dispatch on the main queue

dispatch_async(dispatch_get_main_queue(),{
      self.FirstTextField.roundCorners(UIRectCorner.TopRight | UIRectCorner.TopLeft, radius: 10.0)
      self.SecondTextField.roundCorners(UIRectCorner.BottomLeft | UIRectCorner.BottomRight, radius: 10.0)
});

Now i don't know if this can be inefficient or not but yeah it works :D

if any one has a better Answer please post it :)

Amr Mohamed
  • 2,290
  • 4
  • 20
  • 39
  • This is actually what you're supposed to do if you need to update the UI from somewhere other than the main thread (e.g. downloading an image). Where were you calling the method from before? – David Cao Jul 20 '15 at 16:45
  • i was calling it normally – Amr Mohamed Jul 20 '15 at 17:47
  • I had to do it like this too, making the changes in `viewWillAppear` didn't do anything, and watching an image form into a circle after the interface loaded in `viewDidAppear` is absolutely tacky. Thanks for this solution. – Hobbyist Jan 25 '16 at 00:03
1

Try to move your code of rounding the text fields to - (void)viewDidLayoutSubviews

Alaeddine
  • 6,104
  • 3
  • 28
  • 45
  • Same result as `ViewDidLoad` see the updated question the only difference is that `viewDidLayoutSubviews ` works fine if you rotat the device once @Aladin – Amr Mohamed Jul 20 '15 at 15:53
  • 1
    You mean `viewWillLayoutSubviews` ? @Aladin – Amr Mohamed Jul 20 '15 at 15:56
  • ViewControllers only has `viewWillLayoutSubviews ` so you mean it , but unfortunately same as `ViewDidLoad` and `viewDidLayoutSubviews ` @Aladin – Amr Mohamed Jul 20 '15 at 16:04
0

You'll need to have the layer mask to its bounds

self.layer.masksToBounds = true

EDIT:

Try using & instead of |, I believe it's take only the first corner since you used or.

self.FirstTextField.roundCorners(UIRectCorner.TopRight & UIRectCorner.TopLeft, radius: 10.0)
self.SecondTextField.roundCorners(UIRectCorner.BottomLeft & UIRectCorner.BottomRight, radius: 10.0)
David Cao
  • 525
  • 3
  • 11