gpt4 book ai didi

ios - 移动 View 时安全区域插入消失

转载 作者:行者123 更新时间:2023-11-28 05:41:50 37 4
gpt4 key购买 nike

在我的 iOS 项目中,我有一个带有几个文本字段的 View Controller 。为确保控件在用户输入文本时保持可见,我使用以下代码移动内容并为键盘腾出空间:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}

@objc func keyboardWillShow(notification: NSNotification) {
dPrint(tag: "Safe Area", message: "keyboardWillShow()")
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if activeField != nil {
var activeFieldBottomDistance: CGFloat = view.frame.size.height - ... // calculation of position omitted here
let yOffset: CGFloat = activeFieldBottomDistance
- keyboardSize.height
- view.frame.origin.y
if yOffset + view.frame.origin.y < 0 {
moveViewForKeyboard(offset: yOffset)
}
}
}
}

@objc func keyboardWillHide(notification: NSNotification) {
dPrint(tag: "Safe Area", message: "keyboardWillHide()")
let yOffset: CGFloat = -view.frame.origin.y
moveViewForKeyboard(offset: yOffset)
}

func moveViewForKeyboard(offset: CGFloat!) {
let duration = 0.3
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(duration)
view.frame = view.frame.offsetBy(dx: 0, dy: offset)
UIView.commitAnimations()
view.setNeedsLayout()
view.layoutIfNeeded()
}

布局如下(stack view里面有text views):

Layout

我的问题是,当 Root View 向上移动以为键盘腾出空间时,安全区域边距就会丢失。当横向使用它并且键盘出现时,内容会水平扩展,当键盘再次隐藏时,内容会移回安全区域的边界。

下面的代码会产生下面的输出:

@available(iOS 11.0, *)
override func viewSafeAreaInsetsDidChange() {
super.viewSafeAreaInsetsDidChange()
dPrint(tag: "Safe Area", message: "saveAreaInsetsDidChange")
dPrint(tag: "Safe Area", message: "top: " + String(describing: view.safeAreaInsets.top))
dPrint(tag: "Safe Area", message: "right: " + String(describing: view.safeAreaInsets.right))
dPrint(tag: "Safe Area", message: "bottom: " + String(describing: view.safeAreaInsets.bottom))
dPrint(tag: "Safe Area", message: "left: " + String(describing: view.safeAreaInsets.left))
}

Safe Area saveAreaInsetsDidChange
Safe Area top: 0.0
Safe Area right: 44.0
Safe Area bottom: 21.0
Safe Area left: 44.0
Safe Area keyboardWillShow()
Safe Area saveAreaInsetsDidChange
Safe Area top: 0.0
Safe Area right: 0.0
Safe Area bottom: 0.0
Safe Area left: 0.0
Safe Area keyboardWillHide()
Safe Area saveAreaInsetsDidChange
Safe Area top: 0.0
Safe Area right: 44.0
Safe Area bottom: 21.0
Safe Area left: 44.0

我如何移动内容,使其保持在安全区域内,并且它也适用于从 iOS9 开始的旧设备。

(我尝试过的选项之一是在 Root View 下方创建一个附加 View 层并将其移动。这不起作用,因为像对话框一样以模态方式呈现的内容垂直居中。将中心与除 Root View 不允许内容移动。)

最佳答案

您可以使用 UnderKeyboard实现这一目标的图书馆。

因此,我强烈建议您在打开键盘时使用 UIScrollView 移动您的内容。关注this guide .

或者对底部填充使用 AutoLayout 底部约束并动画更改其常量。

例子:

@IBOutlet var bottomConstraint: NSLayoutConstraint!

override func viewDidLoad() {
super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: "animateWithKeyboard:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "animateWithKeyboard:", name: UIKeyboardWillHideNotification, object: nil)
}

func animateWithKeyboard(notification: NSNotification) {

let userInfo = notification.userInfo!
let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height
let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! UInt
let moveUp = (notification.name == UIKeyboardWillShowNotification)

bottomConstraint.constant = moveUp ? -keyboardHeight : 0

let options = UIViewAnimationOptions(rawValue: curve << 16)
UIView.animateWithDuration(duration, delay: 0, options: options, animations: {
self.view.layoutIfNeeded()
},
completion: nil)
}

关于ios - 移动 View 时安全区域插入消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56399471/

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