0

I have overrided a method as

override func viewDidLayoutSubviews() {

    // creating bottom line for textField
        let border = CALayer()
        let width = CGFloat(1.0)
        border.borderColor = UIColor.whiteColor().CGColor
        border.frame = CGRect(x: 0, y: emailTextField.frame.size.height - width, width:  emailTextField.frame.size.width, height: emailTextField.frame.size.height)

        border.borderWidth = width
        emailTextField.layer.addSublayer(border)
        emailTextField.layer.masksToBounds = true

}

Now whats happening is that when I run my app on Iphone 6, 6+ every thing works fine. But when I run the same code on iphone5 (Simulator + real Device ) viewDidLayoutSubViews is getting called infinite times and my app becoms unresponsive. I solved the problem by using a bool variable. But I do not understand why is this happening. So can someone please explain this to me.

Thanks :-)

Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
Umair Afzal
  • 4,947
  • 5
  • 25
  • 50

3 Answers3

2

As docs says that viewDidLayoutSubviews method need for change layout

Called to notify the view controller that its view has just laid out its subviews. When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout.

Move your code to viewDidLoad method or to loadView if you are not using xibs or Storyboards

override func viewDidLoad() {

    // creating bottom line for textField
        let border = CALayer()
        let width = CGFloat(1.0)
        border.borderColor = UIColor.whiteColor().CGColor
        border.frame = CGRect(x: 0, y: emailTextField.frame.size.height - width, width:  emailTextField.frame.size.width, height: emailTextField.frame.size.height)

        border.borderWidth = width
        emailTextField.layer.addSublayer(border)
        emailTextField.layer.masksToBounds = true

}

If you would like to change border.frame you can set it to class variable and change it in viewDidLayoutSubviews method

override func viewDidLayoutSubviews() {
      self.border.frame = CGRect(x: 0, y: emailTextField.frame.size.height - width, width:  emailTextField.frame.size.width, height: emailTextField.frame.size.height)
      self.border.borderWidth = width    
}
iSashok
  • 2,326
  • 13
  • 21
  • actually problem is solved, But what I do not understand is why the app does not stuck on iphone 6 and 6+ why only on iphone 5 ? – Umair Afzal Sep 08 '16 at 07:53
  • I think the diff between iOS versions and ram memory limits in devices – iSashok Sep 08 '16 at 08:00
  • 1
    @UmairAfzal your issue is not solved. You are using a very bad practise to solve this problem, blocking the method from running is never the right choice. `viewDidLayoutSubviews` is for positioning views, not creating and adding them. Please move this code to viewDidLoad as suggested above. You will run into this problem every time otherwise – Simon McLoughlin Sep 08 '16 at 08:27
0

Just replace

emailTextField.layer.addSublayer(border)

with this:

emailTextField.addSublayer(border)

-> you want to add sublayer to a view - as you want to apply masksToBounds property to it either.

pedrouan
  • 12,762
  • 3
  • 58
  • 74
0

Move your implementation inside viewDidLayoutSubviews. I got the same issue. Hope it will work.

override func viewDidLayoutSubviews() 
{
 //Your CODE    
}
Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49