0

IB through the cell UITableViewCell added UISWitch. When programmatically setting text UILabel, UISwitch overlaps.

How can this be avoided?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

1

add constraint, that shows distance between them is greater than 0

b m gevariya
  • 300
  • 1
  • 4
  • 12
0

Get size of text in UILabel and then move UISwitch to new position.

Reconquistador
  • 885
  • 8
  • 28
0

If you're placing the switch to the right of your UILabel, you can just set it as the accessory view of the cell in your code:

let switch = UISwitch()
// Position the switch to the trailing edge of the cell
cell.accessoryView = switch 
// Tell the text label to reduce text size if the switch gets in the way
cell.textLabel?.adjustsFontSizeToFitWidth = true 

This way, you won't have to deal with positioning / auto layout. I believe this is possible to do in Interface Builder as well -- see https://stackoverflow.com/a/45849911/1646862 (in that case, make sure you set the Autoshrink property of the label)

Hristo
  • 6,382
  • 4
  • 24
  • 38