具有.compact样式的UIDatePicker不尊重包含优先权的内容 [英] UIDatePicker with .compact style doesn't respect content hugging priority

查看:109
本文介绍了具有.compact样式的UIDatePicker不尊重包含优先权的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为具有.compact样式的新UIDatePicker(iOS 14+)在AutoLayout中无法正常使用.

I think that the new UIDatePicker (iOS 14+) with the .compact style doesn't work correctly with AutoLayout.

使用UILabelUIDatePicker进行简单的布局,其中UIDatePicker具有更高的内容拥抱优先级,如果可用空间过多,则会导致拉伸UILabel的布局,但这不是会发生什么.

Making a simple layout with a UILabel and UIDatePicker where the UIDatePicker has higher content-hugging priority should result in a layout where the UILabel is being stretched if there is too much space available but that's not what happens.

这就是我得到的结果:

这是示例代码:

class ViewController: UIViewController, UIGestureRecognizerDelegate {
    private let label: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.text = "Label"
        label.backgroundColor = .yellow
        return label
    }()
    
    private let datePicker: UIDatePicker = {
        let datePicker = UIDatePicker()
        datePicker.translatesAutoresizingMaskIntoConstraints = false
        datePicker.preferredDatePickerStyle = .compact
        return datePicker
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(label)
        view.addSubview(datePicker)
        
        label.setContentHuggingPriority(.defaultLow, for: .horizontal)
        datePicker.setContentHuggingPriority(.defaultLow + 1, for: .horizontal)
        
        NSLayoutConstraint.activate([
            label.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8),
            label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 8),
            
            label.bottomAnchor.constraint(equalTo: datePicker.bottomAnchor),
            label.trailingAnchor.constraint(equalTo: datePicker.leadingAnchor, constant: -8),
            
            datePicker.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8),
            datePicker.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -8)
        ])
    }
}

我错过了什么吗?这是一个已知问题吗?

Am I missing something? Is this a known issue?

推荐答案

对我来说很好.将标签的水平内容包含优先级设置为.defaultLow-1.

Works fine for me. Set the label's horizontal content hugging priority to .defaultLow-1.

这篇关于具有.compact样式的UIDatePicker不尊重包含优先权的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆