2

I have an issue with the button click events on an old app. Everything works OK for iOS versions < 13, but on iOS 13 the button clicks are not working.

This is the code that generates the buttons:

override func layoutSubviews() {
     super.layoutSubviews()

     var offsetY = CGFloat(0)

     for view in authenticationViews {
         view.button.frame = CGRect(x: 0, y: offsetY, width: frame.size.width, height: buttonHeight)
         offsetY += buttonsDistance + buttonHeight
         addSubview(view.button)
    }

    height = offsetY
}

I think there might be an issue with the constraints, but I am new to iOS and I don't know what to uderstand from this error and how should be fixed:

Will attempt to recover by breaking constraint (null) ((null), (null))

Laura
  • 2,653
  • 7
  • 37
  • 59
  • 2
    What you can do is use the visual debugger to inspect the generated constraints in runtime. Also, adding subviews in `layoutSubviews` could be avoided since the method can be called multiple times. Finally, you could also put a breakpoint at the action method to ensure that the problem is indeed what you suspect it to be (how do you add the target/action btw?) – Alladinian Apr 22 '20 at 10:50
  • Where is target Action? Also share screenshot – Muhammad Hasan Irshad Apr 24 '20 at 09:50
  • You can use the [Snapkit](https://github.com/SnapKit/SnapKit) or any other way in order to make constraints easily. I guess you need to make constaratins like this: `view.button.snp.makeConstraints { make in make.width.equalTo(frame.size.width) make.height.equalTo(buttonHeight) }` – Reza Dehnavi Apr 27 '20 at 09:19

1 Answers1

0

It would be great if you provide the full code but based on the code provided here're the suggestions.

Since the layoutSubviews function can be called multiple times, you should move them inside viewDidLoad.

override func viewDidLoad() {
    ...
    for view in authenticationViews {
        addSubview(view.button)
    }
}

To update the frames of the views based on the layout change, you have to put them in viewDidLayoutSubviews

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    var offsetY = CGFloat(0)

    for view in authenticationViews {
        view.button.frame = CGRect(x: 0, y: offsetY, width: frame.size.width, height: buttonHeight)
        offsetY += buttonsDistance + buttonHeight
    }

    height = offsetY
}
Jun
  • 3,422
  • 3
  • 28
  • 58