iphone X 的 TraitCollectionDidChange 行为不同

TraitCollectionDidChange behaves different for iphone X

在我的例子中,我正在根据 traitCollection.horizontalSizeClass 更改我的视图布局 这是我的代码片段。

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)

        if traitCollection.horizontalSizeClass == .regular {
            // 2
            NSLayoutConstraint.deactivate(compactConstraints)
            NSLayoutConstraint.activate(regularConstraints)
            // 3
            socialMediaView.axis = .horizontal
        } else {
            // 4
            NSLayoutConstraint.deactivate(regularConstraints)
            NSLayoutConstraint.activate(compactConstraints)
            socialMediaView.axis = .vertical
        }
    }

一切都按照 iphone 7 plusiphone x 中的指示工作 在横向模式下,我希望兔子图像出现在左侧,并且所有社交媒体轴的堆栈视图都是水平的
但在 iphone X 横向模式下不会出现,而在 phone 7 中它会出现。检查下面的屏幕截图

看了你的问题和情况,我发现你的情况有问题。您应该检查 verticalSizeClass 而不是 horizo​​ntalSizeClass.

检查水平尺寸时 CLASS。

纵向: 所有iPhone设备的宽度都很紧凑,所以每次都会转到其他条件并正确设置视图。

IN LANDSCAPE:所有 iPhone Plus (iPhone 6s Plus,iPhone 7 Plus 和 iPhone 8 plus) 设备具有常规宽度和所有其他 iPhone (iPhone 6s, 6SE, iPhone 7 and iPhone 8 , iPhone X) 设备具有紧凑的宽度,因此对于所有 plus 设备都可以正常工作,但对其他设备则不行。

肖像:

对于风景:

更多内容,阅读官方文档here

所以,将您的代码更新为这个。

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)

        if traitCollection.verticalSizeClass == .regular {
            NSLayoutConstraint.deactivate(compactConstraints)
            NSLayoutConstraint.activate(regularConstraints)
            socialMediaView.axis = .horizontal
        } else {
            NSLayoutConstraint.deactivate(regularConstraints)
            NSLayoutConstraint.activate(compactConstraints)
            socialMediaView.axis = .vertical
        }
    }

尝试并分享结果。