0

I have the following screen, in order to centre the boxes between the uilabel and the keyboard I created a simple calculation where I added the header height then the uilabel height and then the keyboard height minus the whole screen size and divided everything by two in order to get the centre point [i added the code below although it's not relevant]

The issue is that in order to get the UILabels height I have to set the constraints in viewDidLayoutSubviews(). Now the idea is that every time you fill one box the next box becomes the first responder but that doesn't work because every time a box becomes a first responder viewDidLayoutSubviews() is being called.

What can I do about this ? is there a way to get the height of uiLabel without calling viewDidLayoutSubviews().

This is the code for centering the boxes :

    override func viewDidLayoutSubviews() {
    let allItems = headerC.frame.height + stackViewOfDefin.frame.height + 270 + 20
    let getSpace = UIScreen.main.bounds.height - allItems
    let stackview = UIStackView(arrangedSubviews: gameBoxes)
    stackview.axis = .horizontal
    stackview.spacing = 5
    stackview.distribution = .fillEqually
    stackview.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(stackview)
    NSLayoutConstraint.activate([
        stackview.topAnchor.constraint(equalTo: secondLine.bottomAnchor, constant: getSpace/2-30),
        stackview.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
        stackview.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 10),
        stackview.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: -10)
        ])
}

enter image description here

Neta yam
  • 69
  • 5
  • 1
    You might be able to just center the `UILabel` when you have the keyboard appear/disappear. Ways to listen for the keyboard hooks can be found here https://stackoverflow.com/questions/4374436/how-to-detect-when-keyboard-is-shown-and-hidden. There are other questions for that too though – Bram Apr 20 '20 at 13:07
  • 1
    Is this keyboard an `inputView` (i.e. a custom keyboard that shows and hides, replacing the standard iOS one) or is just a custom view that's always visible? – Pete Morris Apr 20 '20 at 13:35
  • it's an input view, added it as subview and looks like it solved the issue, cheers – Neta yam Apr 20 '20 at 13:56

0 Answers0