How to center UILabel in the Content View of a tableview cell?

I'm having trouble centring a UILabel in a tableview cell. I set the constraints in a prototype cell, and everything seems to be okay, but when running the label actually shows up at the left side of the cell. Any idea why this happens and how to fix it?

How it looks like in xCode enter image description here

and when run on my iPhone enter image description here

3 answers

  • answered 2018-10-21 11:15 Md Rashed Pervez

    At first select the label and then go to Align and select Vertically in Container. It will do the work for you.

    enter image description here

    Programatically you can do -

    yourlabelName.textAlignment =


    cell.textLabelName?.textAlignment = .center

  • answered 2018-10-21 11:18 m57

    It looks like you fill wrong label. Make sure you set value to myLabel, cell.myLabel.text = "9:00", and not to cell.textLabel?.text.

  • answered 2018-10-21 11:22 Zsolt B. Horcsik

    As much as I hate to answer my own question, but I will leave it here, maybe helps someone in the future...

    The problem was solved using this. The tableview cell had its own class. The content view had to be set to UIView instead of the custom class. The cell itself still keeps the properties of your custom class.