0

I've been trying to vertically centre a UILabel across all devices, both when the keyboard isn't visible and when it is visible.

I previously tried to adjust the top constraint by doing this: self.emptyStateMessageTopConstraint.constant = (self.view.frame.size.height / 2 ) - (self.emptyStateMessageHeight.constant / 2 )

This should have given me the mid point when the keyboard wasn't visible.

I did this when the keyboard was visible:

self.emptyStateMessageTopConstraint.constant = (self.view.frame.size.height - keyboardHeight) / 2 - self.emptyStateMessageHeight.constant / 2

In theory, it should have worked but it wasn't entirely vertically centre.

As you can see below, it's not centred.

enter image description here

I scrapped that idea and tried to centre vertically. It worked perfectly across all devices when the keyboard is not visible as seen below.

enter image description here

Now my issue is, how can I programmatically change the vertical constant when the keyboard is visible across all devices.

I'm struggling since the calculation I used previously can't really be used to change the vertical constant.

daanyyaal
  • 301
  • 2
  • 4
  • 16

1 Answers1

0

If you see the value of center constraint layout you will find it equal 0 so that determine the position of center constraint is multiplier not constant try to apply your calculations on multiplier property , but you will find multiplier property is read only so you cannot to modify on it , but there is work around for that , I hope that helpe you .

Hosny
  • 821
  • 1
  • 13
  • 25