我正在开发一项功能,该功能将从屏幕底部向上显示 UIView
动画,以告诉用户他们的数据已保存。不过,它在 iPhone 11 Pro Max 上的显示位置与 iPhone X 上的不同。
iPhone 11 Pro Max(这就是我想要的样子):
但在 iPhone X 上:
我可能应该注意到,我在 Storyboard 上创建了 View ,并有一个 @IBOutlet
连接到它。在 Storyboard上,它位于您在顶部屏幕截图中看到的位置。我没有对其施加任何限制。其余的工作由代码完成。
动画代码非常简单:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
savedView.center.y += view.bounds.height // start with it off the screen
savedView.center.x = (view.bounds.width / 2)
}
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(true)
// animate it onto the screen
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.0, options: [], animations: {
self.savedView.center.y -= self.view.bounds.height <-- the problem must be here
}, completion: nil)
}
我认为这可能是由于不同的设备尺寸类别造成的,但所有 iPhone 都是“常规”高度,所以我认为不是这样的。
如何让此 View 在所有设备上最终位于相同位置(相对于屏幕底部)?
最佳答案
为了将您的 View 放置在所有设备上的同一位置,必须设置约束。这样,系统就能够正确地显示您的观点。
因此,要为 View 实现底部居中的附加行为,您需要对安全区域底部设置约束,该约束称为水平居中 、宽度和高度,假设此 View 是屏幕上的唯一组件。如果不是,则组件位于顶部的 View 必须有一个顶部约束。
希望这对您有所帮助,至少给您一个想法。
关于ios - 如何使所有设备上的动画 View 与屏幕底部的距离保持一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59111388/