快速更改方向后为什么顶部锚不再起作用?

问题描述 投票:0回答:1

我已经以编程方式创建了UI组件。从横向旋转到纵向后,顶部锚常数44不再起作用。

屏幕截图:

“”

“”

“”

代码:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.userInterfaceIdiom == .phone {
        if UIDevice.current.orientation.isPortrait {
            topLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 44).isActive = true
        } else {
            topLabel.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        }
    } else {
        topLabel.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    }
}
swift ios9 nslayoutconstraint landscape portrait
1个回答
0
投票

尝试使topLabel.topAnchor等于self.safeAreaLayoutGuide.topAnchor而不是view.topAnchor

© www.soinside.com 2019 - 2024. All rights reserved.