设备方向更改期间,UILabel在模式中出现奇怪的过渡 [英] Weird transition of UILabel in modal during device orientation change

查看:78
本文介绍了设备方向更改期间,UILabel在模式中出现奇怪的过渡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自动布局约束使UILabel停留在模式的顶部.

I'm using autolayout constraints to make an UILabel stay at the top of a modal.

title.topAnchor.constraint(equalTo: view.topAnchor, constant: 10)
title.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 5)
title.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: -5)
title.centerXAnchor.constraint(equalTo: view.centerXAnchor)

模态显示在屏幕中央,其preferredContentSize硬编码为312宽度和219高度.

The modal is presented at the center of the screen and its preferredContentSize is hardcoded as 312 width and 219 height.

但是,在我的iPhone 11上,当我的设备从纵向旋转到横向旋转时,模态首先变白,并且UILabel在旋转完成之前会飞入视图中(img 1).但是,当我的设备从横向旋转回纵向时,UILabel随屏幕旋转到位,并且永远不会离开模态(img 2).为什么会有这样的差异?如何在方向更改期间始终使UILabel旋转到位?

However, on my iPhone 11, when my device is being rotated from portrait to landscape, the modal is whiteout firstly, and the UILabel flies into the view before rotation completes(img 1). But when my device is being rotated from landscape back to portrait, the UILabel rotates in place with the screen and never leaves the modal(img 2). Why there is such difference? What can I do to always make the UILabel rotates in place during the orientation change?

我觉得这可能与我的约束有关,但我不知道为什么.有人可以帮忙一下吗?谢谢!

I feel like it might be related to my constraints but I couldn't figure out why. Could someone please help shed some light? Thanks!

推荐答案

您的约束似乎很好,您只需激活这些约束即可.

Your constraints seem fine, you just have to activate those constraints.

NSLayoutConstraint.activate([
    title.topAnchor.constraint(equalTo: view.topAnchor, constant: 10)
    title.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 5)
    title.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: -5)
    title.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])

这篇关于设备方向更改期间,UILabel在模式中出现奇怪的过渡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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