gpt4 book ai didi

swift - TraitCollectionDidChange 对 iphone X 的行为有所不同

转载 作者:行者123 更新时间:2023-11-28 05:54:51 25 4
gpt4 key购买 nike

在我的例子中,我正在根据 traitCollection.horizo​​ntalSizeClass 更改我的 View 布局这是我的代码片段。

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 纵向模式下的所有功能都按照指示工作 Screen_Shot_2018-08-07_at_1.44.54_PM.png在横向模式下,我希望兔子图像出现在左侧,并且所有社交媒体轴的堆栈 View 都是水平的
但在 iphone X 横向模式下它不会出现,而在 phone 7 中它会出现。检查下面的屏幕截图 Screen_Shot_2018-08-07_at_1.32.02_PM.png

最佳答案

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

何时检查水平尺码等级。

IN PORTRAIT:所有 iPhone 设备的宽度都很紧凑,因此每次都会转到其他条件并正确设置 View 。

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

肖像: Portrait

对于景观: Landscape

有关更多信息,请阅读官方文档 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
}
}

尝试并分享结果。

关于swift - TraitCollectionDidChange 对 iphone X 的行为有所不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51721797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com