I am creating a normal UILabel and setting it as tableViewCell's accessoryView. My understanding is, accessoryView stays vertically centre aligned inside cell. But that is not happening. As I decrease the label text's font, the accessory view moves more upwards. Button works just fine. Problem with UILabel.
Here is my code :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = "Hello"
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 42, height: 21))
label.backgroundColor = UIColor.red
label.font = UIFont(name: "Helvetica Neue", size: 12)
label.highlightedTextColor = UIColor.white
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.green
label.textAlignment = .right;
label.clipsToBounds = true
label.autoresizesSubviews = true
label.contentMode = .left
label.text = "123";
cell.accessoryView = label
return cell
}
I found a solution. The frame that I was giving to UILabel was CGRect(x: 0, y: 0, width: 42, height: 21). If I give the height same as the text size i.e. 12.0, The label becomes centre aligned :). This seems like an iOS 13 issue. Not seen in iOS 12 devices.
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 42, height: 12))