gpt4 book ai didi

ios - 为什么当键盘隐藏时整个 View 会跳起来?

转载 作者:行者123 更新时间:2023-11-28 14:22:17 32 4
gpt4 key购买 nike

当调用 keyboardWillHideFunction 时,viewController 中的整个 View 会向上猛拉。以下是问题发生的视频。

vidoe of screen jerking

下面是我用来控制键盘通知的代码。我试过玩弄动画时间和布局限制,但我没能走得更远。关于如何解决它的任何建议谢谢。

根据建议编辑代码 - 我已经尝试按照建议为布局约束创建一个变量,但是问题仍然存在,为什么?

import UIKit
import Foundation

extension UIView {
func currentFirstResponder() -> UIResponder? {
if self.isFirstResponder {
return self
}

for view in self.subviews {
if let responder = view.currentFirstResponder() {
return responder
}
}

return nil
}
}

extension Notification.Name{
static let showKeyboard = Notification.Name("showKeyboard")
}

class KeyboardSlider: NSObject {
// variables to hold and process information from the view using this class
weak var view: UIView?
var searchBarTopTags:SearchBarTopTagsViewController?
var amountToShiftBy:CGFloat!
var originalFrame:CGRect!
var previewController:PreviewController!

@objc func keyboardWillShow(notification: NSNotification) {
self.searchBarTopTags?.myViewBottomLayoutConstraint.constant = -self.getKeyboardHeight(notification as! Notification) + previewController.view.safeAreaInsets.bottom
UIView.animate(withDuration: 0, animations: {
self.view?.layoutIfNeeded()
self.searchBarTopTags?.myView.layoutIfNeeded()

})
}

@objc func keyboardWillHide(notification:NSNotification){

self.searchBarTopTags?.myViewBottomLayoutConstraint.constant = 0
UIView.animate(withDuration: 0, animations: {
self.view?.layoutIfNeeded()
self.searchBarTopTags?.myView.layoutIfNeeded()
})
}
func getKeyboardHeight(_ notification:Notification) -> CGFloat {
// get exact height of keyboard on all devices and convert to float value to return for use
let userInfo = notification.userInfo
let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
return keyboardSize.cgRectValue.height
}

func subscribeToKeyboardNotifications(view: UIView) {
// assigning view to class' counterpart
self.view = view
// when UIKeyboardWillShow do keyboardWillShow function
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
}


func subscribeToKeyboardNotifications(view: UIView, previewController:PreviewController? = nil) {
// assigning view to class' counterpart
self.view = view
self.searchBarTopTags = previewController?.searchBarTopTags
self.previewController = previewController

// when UIKeyboardWillShow do keyboardWillShow function
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)
}


func unsubscribeFromKeyboardNotifications() {
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}

}

原始代码:

import UIKit
import Foundation

extension UIView {
func currentFirstResponder() -> UIResponder? {
if self.isFirstResponder {
return self
}

for view in self.subviews {
if let responder = view.currentFirstResponder() {
return responder
}
}

return nil
}
}

extension Notification.Name{
static let showKeyboard = Notification.Name("showKeyboard")
}

class KeyboardSlider: NSObject {
// variables to hold and process information from the view using this class
weak var view: UIView?
var searchBarTopTags:SearchBarTopTagsViewController?
var amountToShiftBy:CGFloat!
var originalFrame:CGRect!

@objc func keyboardWillShow(notification: NSNotification) {
self.originalFrame = self.searchBarTopTags?.myView.frame
self.amountToShiftBy = (self.searchBarTopTags?.view.frame.maxY)! - self.getKeyboardHeight(notification as! Notification) - (self.searchBarTopTags?.myView.frame.height)!
self.amountToShiftBy = (searchBarTopTags?.view.bounds.height)! - self.getKeyboardHeight(notification as! Notification) - (searchBarTopTags?.myView.bounds.height)!
self.searchBarTopTags?.myView.bottomAnchor.constraint(equalTo: (self.searchBarTopTags?.view.bottomAnchor)!, constant: -self.amountToShiftBy).isActive = true

UIView.animate(withDuration: 0, animations: {
self.view?.layoutIfNeeded()
self.searchBarTopTags?.view.layoutIfNeeded()
self.searchBarTopTags?.myView.layoutIfNeeded() })

}

@objc func keyboardWillHide(notification:NSNotification){

self.searchBarTopTags?.myView.bottomAnchor.constraint(equalTo: (self.searchBarTopTags?.view.bottomAnchor)!, constant: 0).isActive = true
UIView.animate(withDuration: 0, animations: {
self.view?.layoutIfNeeded()
self.searchBarTopTags?.view.layoutIfNeeded()
self.searchBarTopTags?.myView.layoutIfNeeded()
})
}
func getKeyboardHeight(_ notification:Notification) -> CGFloat {
// get exact height of keyboard on all devices and convert to float value to return for use
let userInfo = notification.userInfo
let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
return keyboardSize.cgRectValue.height
}

func subscribeToKeyboardNotifications(view: UIView) {
// assigning view to class' counterpart
self.view = view
// when UIKeyboardWillShow do keyboardWillShow function
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
}


func subscribeToKeyboardNotifications(view: UIView, seachBarTopTagsVC:SearchBarTopTagsViewController? = nil) {
// assigning view to class' counterpart
self.view = view
self.searchBarTopTags = seachBarTopTagsVC

// when UIKeyboardWillShow do keyboardWillShow function
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)
}


func unsubscribeFromKeyboardNotifications() {
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}

}

最佳答案

keyboardWillShow 里面有

self.searchBarTopTags?.myView.bottomAnchor.constraint(equalTo: (self.searchBarTopTags?.view.bottomAnchor)!, constant: -self.amountToShiftBy).isActive = true

keyboardWillHide 里面

self.searchBarTopTags?.myView.bottomAnchor.constraint(equalTo: (self.searchBarTopTags?.view.bottomAnchor)!, constant: 0).isActive = true

当键盘隐藏/显示时,这当然会引起冲突,你只需要在 viewDidLoad 中创建一次底部约束,然后在这些方法中使用常量值,就像这样

var bottomConstraint:NSLayoutConstraint!

//

bottomConstraint = self.searchBarTopTags?.myView.bottomAnchor.constraint(equalTo: (self.searchBarTopTags?.view.bottomAnchor)!, constant: 0)
bottomConstraint.isActive = true

//

@objc func keyboardWillShow(notification: NSNotification) {

// other code
bottomConstraint.constant = -self.amountToShiftBy
}

@objc func keyboardWillHide(notification: NSNotification) {

bottomConstraint.constant = 0
// other code
}

关于ios - 为什么当键盘隐藏时整个 View 会跳起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51831055/

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