简体   繁体   中英

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 在此处输入图片说明

and when run on my iPhone 在此处输入图片说明

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

在此处输入图片说明

Programatically you can do -

yourlabelName.textAlignment = NSTextAlignment.center

or

cell.textLabelName?.textAlignment = .center

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM