UIDatePicker not respecting autolayout constraints iOS 14

Viewed 567

The new UIDatePicker style's doesn't appear to be respecting autolayout. It should be right aligned with the label expanding to take up the majority of the cell. Here is the code for the cell.

class DatePickerTableViewCell: UITableViewCell {

    public let label = UILabel()
    public let datePicker = UIDatePicker()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // Cell settings
        self.label.translatesAutoresizingMaskIntoConstraints = false
        self.datePicker.translatesAutoresizingMaskIntoConstraints = false
        
        // Add views
        self.contentView.addSubview(self.label)
        self.contentView.addSubview(self.datePicker)
        
        // Constrain views
        self.label.setContentHuggingPriority(.defaultLow - 1, for: .horizontal)
        self.datePicker.setContentHuggingPriority(.defaultLow + 1, for: .horizontal)
        
        NSLayoutConstraint.activate([
            self.label.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
            self.label.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
            self.label.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor),
            self.label.trailingAnchor.constraint(equalTo: self.datePicker.leadingAnchor, constant: -8),
            
            self.datePicker.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
            self.datePicker.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor),
            self.datePicker.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor)
        ])
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

Here are some pictures showing what it is doing:

Compact date picker in cell not right aligning Compact date picker in cell not right aligning with colour blended layers on

This is similar to this stack overflow question but doesn't appear to be fixed by their solution UIDatePicker with .compact style doesn't respect content hugging priority

0 Answers
Related