Jonnny Jonnny - 1 year ago 97
Swift Question

UILabel was circular, now has become eliptical

I was using the following code in swift:

label.layer.cornerRadius = CGRectGetWidth(label.frame) / 2
label.layer.masksToBounds = true

This worked well, but I then made my row height taller, it was 44 and then I made it 66 tall. Now the circular label is elliptical.
enter image description here

I notice that when I make
is false I can see the label's background is more of a rectangle. It's wide and not as tall as the label is even though it's dimensions are set to be square. What am I doing wrong?

enter image description here

Here are my constraints:
enter image description here

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
I am calling a function called Configure cell which takes a UITableViewCell and just updates the text.

func configureTextForCell(cell: UITableViewCell, withPlayer player: Player){
let jerseyLabel = cell.viewWithTag(3000) as! UILabel
jerseyLabel.text = String(player.jerseyNumber)
circleLabels(jerseyLabel, hex: "#6470FF")


is like this:

func circleLabels(label: UILabel, hex: String){
label.layer.cornerRadius = label.frame.size.width/2
label.layer.masksToBounds = true

Sam Sam
Answer Source

It seems to be an issue with when the call is made compared to when the cell is laid out. An easy fix is to just make the cell the same size in both the storyboard and in code


or in the delegate function)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download