gpt4 book ai didi

ios - Swift:如何根据设备垂直居中 View /标签?

转载 作者:可可西里 更新时间:2023-11-01 00:57:04 25 4
gpt4 key购买 nike

当键盘在所有设备(iPhone 5 - 7 Plus)上都可见时,我试图将标签垂直居中。

目前,该标签仅在 iPhone 5 屏幕上垂直居中,但这只是因为我使用以下代码手动设置了它:

    func keyboardWillShow(_ notification:Notification) {

let userInfo:NSDictionary = (notification as NSNotification).userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height

UIView.animate(withDuration: 0.3) {
self.emptyStateMessageTopConstraint.constant = keyboardHeight - 145
self.updateViewConstraints()
self.emptyDataInfoView.layoutIfNeeded()
}

我的猜测是在键盘可见时找到 UIView 的大小,并根据设备调整自动布局约束。

我不知道这是否正确,但我正在努力寻找方法。

使用上述函数执行以下操作:

iPhone 5/SE:

下图,您可以看到描述居中。 enter image description here

iPhone 7 Plus:

下图,您可以看到描述没有居中。 enter image description here

最佳答案

将此行更改为

self.emptyStateMessageTopConstraint.constant = keyboardHeight - 145

这个

self.emptyStateMessageTopConstraint.constant = (self.view.frame.size.height - keyboardHeight)/2 - yourlabelheight/2

关于ios - Swift:如何根据设备垂直居中 View /标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44631322/

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