IB through the cell UITableViewCell
added UISWitch
. When programmatically setting text UILabel
, UISwitch
overlaps.
How can this be avoided?
IB through the cell UITableViewCell
added UISWitch
. When programmatically setting text UILabel
, UISwitch
overlaps.
How can this be avoided?
Get size of text in UILabel and then move UISwitch to new position.
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)