Ios 定义SnapKit值时,CGAffineTransform旋转会导致在宽度和高度之间切换

Ios 定义SnapKit值时,CGAffineTransform旋转会导致在宽度和高度之间切换,ios,autolayout,snapkit,Ios,Autolayout,Snapkit,我已经相当习惯于使用SnapKit,但我不确定如何解决这个问题 这就是我当前的UI外观: 为了实现这一目标,我要: override func viewDidLoad() { super.viewDidLoad() regionsPicker.snp.makeConstraints { (make) in make.centerX.equalToSuperview() make.height.equalToSuperview(

我已经相当习惯于使用SnapKit,但我不确定如何解决这个问题

这就是我当前的UI外观:

为了实现这一目标,我要:

 override func viewDidLoad() {
        super.viewDidLoad()

 regionsPicker.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalToSuperview() // since we rotate (this is the width)
            make.top.equalTo(regionsLabel.snp.bottom).offset(15)
            make.width.equalTo(100)
        }
 rotationAngle = -90 * (.pi/180)
        regionsPicker.transform = CGAffineTransform(rotationAngle: rotationAngle)
}
如上所示,我正在尝试将标签的底部与选择器视图的顶部对齐。但是,由于我旋转我的视图,使高度等于superview,它反而创建了这个巨大的空间

我已经看到有人建议在这种特定情况下使用autolayout,或者,我也可以使用collection view,但我很想知道是否有一个SnapKit解决方案可以解决这个问题


感谢您的帮助。

问题在于苹果文档中对转换的描述的这一部分:

在iOS 8.0及更高版本中,“变换”属性不影响自动布局。“自动布局”根据未转换的边框计算视图的对齐矩形。 因此,更改标签的文本时,约束与未转换的框架相关

因此,您正在将pickerView的高度设置为视图的高度。然后旋转它,它看起来就像你想要的样子

但是,如果使用“调试视图层次结构”,您将看到pickerView现在已远远超出其superView的前缘和后缘

您还将看到iPhone 8上pickerView-100x667的边界宽度和高度,这不是您所期望的

当视图大小更改(如设备旋转)时,这也会出现问题

以下是一个示例方法:

import UIKit

class ViewController: UIViewController {

    let regionsLabel: UILabel = {
        let v = UILabel()
        v.backgroundColor = .red
        v.text = "Testing"
        return v
    }()

    let regionsPicker: UIPickerView = {
        let v = UIPickerView()
        v.backgroundColor = .green
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .cyan

        // add the label
        view.addSubview(regionsLabel)

        // center label horizontally, 10-pts from view top (safe area)
        regionsLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(10)
        }

        // add the picker view
        view.addSubview(regionsPicker)

        // rotate the picker view
        let rotationAngle: CGFloat = -90 * (.pi/180)
        regionsPicker.transform = CGAffineTransform(rotationAngle: rotationAngle)

    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        regionsPicker.snp.remakeConstraints { (make) in

            // constrain picker view center horizontally
            make.centerX.equalToSuperview()

            // constant of 100 - because it's rotated, set the width
            make.width.equalTo(100)

            // constrain the height to a constant equal to the view width
            make.height.equalTo(view.bounds.width)

            // constrain the centerY to the centerY of the label,
            // plus one-half the label's frame height / 2
            // plus a constant of 65 (half the "height" of the picker view + 15-pts spacing)
            make.centerY.equalTo(regionsLabel.snp.centerY).offset(regionsLabel.frame.height / 2.0 + 65.0)

        }

    }

}
其结果是:


谢谢您的详细回答!