Ios 处理具有底部约束的键盘
我正在构建一个聊天应用程序,它具有常规的设计和聊天应用程序的用户界面 我有一个工具栏,我的应用程序需要0,0 375x66,我希望它保持在键盘显示时的位置。它有4个约束:顶部:0、前导:0、尾随:0、纵横比:125:22 我有一个UICollectionView 0,66 375x530,当显示键盘时,我想像任何其他聊天应用程序一样滚动它。它有4个约束:从顶部到工具栏:0,前导:0,尾随:0,从底部到newMessageView,我将简要介绍:0 我有一个UIView,里面有一个UIExtField。我们称之为newMessageView 0596 375x71,它有4个约束:底部:0、前导:0、尾随:0、纵横比:375:71 现在,我正在尝试在显示键盘时使用newMessageView应用程序。我正试着这样做:Ios 处理具有底部约束的键盘,ios,iphone,swift,cocoa-touch,uiview,Ios,Iphone,Swift,Cocoa Touch,Uiview,我正在构建一个聊天应用程序,它具有常规的设计和聊天应用程序的用户界面 我有一个工具栏,我的应用程序需要0,0 375x66,我希望它保持在键盘显示时的位置。它有4个约束:顶部:0、前导:0、尾随:0、纵横比:125:22 我有一个UICollectionView 0,66 375x530,当显示键盘时,我想像任何其他聊天应用程序一样滚动它。它有4个约束:从顶部到工具栏:0,前导:0,尾随:0,从底部到newMessageView,我将简要介绍:0 我有一个UIView,里面有一个UIExtFie
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.newMessageViewBottomConstraint.constant += keyboardSize.height
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.newMessageViewBottomConstraint.constant -= keyboardSize.height
}
}
}
但它完全不起作用。景色跳跃和隐藏,我真的不明白为什么
我做得对吗?有人能帮我并指导我吗?你必须使用更新后需要的self.view.layoutifneed
我为你写完整的解决方案
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWillShow(notification: Notification) {
self.keyboardControl(notification, isShowing: true)
}
@objc func keyboardWillHide(notification: Notification) {
self.keyboardControl(notification, isShowing: false)
}
private func keyboardControl(_ notification: Notification, isShowing: Bool) {
/* Handle the Keyboard property of Default*/
var userInfo = notification.userInfo!
let keyboardRect = (userInfo[UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue
let curve = (userInfo[UIKeyboardAnimationCurveUserInfoKey]! as AnyObject).uint32Value
let convertedFrame = self.view.convert(keyboardRect!, from: nil)
let heightOffset = self.view.bounds.size.height - convertedFrame.origin.y
let options = UIViewAnimationOptions(rawValue: UInt(curve!) << 16 | UIViewAnimationOptions.beginFromCurrentState.rawValue)
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey]! as AnyObject).doubleValue
var pureheightOffset : CGFloat = -heightOffset
if isShowing { /// Wite space of save area in iphonex ios 11
if #available(iOS 11.0, *) {
pureheightOffset = pureheightOffset + view.safeAreaInsets.bottom
}
}
// Here change you Consrant
// self.actionBarPaddingBottomConstranit?.update(offset:pureheightOffset)
UIView.animate(
withDuration: duration!,
delay: 0,
options: options,
animations: {
self.view.layoutIfNeeded()
},
completion: { bool in
})
}
更新约束后必须使用self.view.layoutifneed
我为你写完整的解决方案
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWillShow(notification: Notification) {
self.keyboardControl(notification, isShowing: true)
}
@objc func keyboardWillHide(notification: Notification) {
self.keyboardControl(notification, isShowing: false)
}
private func keyboardControl(_ notification: Notification, isShowing: Bool) {
/* Handle the Keyboard property of Default*/
var userInfo = notification.userInfo!
let keyboardRect = (userInfo[UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue
let curve = (userInfo[UIKeyboardAnimationCurveUserInfoKey]! as AnyObject).uint32Value
let convertedFrame = self.view.convert(keyboardRect!, from: nil)
let heightOffset = self.view.bounds.size.height - convertedFrame.origin.y
let options = UIViewAnimationOptions(rawValue: UInt(curve!) << 16 | UIViewAnimationOptions.beginFromCurrentState.rawValue)
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey]! as AnyObject).doubleValue
var pureheightOffset : CGFloat = -heightOffset
if isShowing { /// Wite space of save area in iphonex ios 11
if #available(iOS 11.0, *) {
pureheightOffset = pureheightOffset + view.safeAreaInsets.bottom
}
}
// Here change you Consrant
// self.actionBarPaddingBottomConstranit?.update(offset:pureheightOffset)
UIView.animate(
withDuration: duration!,
delay: 0,
options: options,
animations: {
self.view.layoutIfNeeded()
},
completion: { bool in
})
}
你能分享故事板视图层次结构的屏幕截图吗。我认为你的文本视图隐藏在其他视图后面。你在控制台上看到任何与约束相关的错误吗?@ParthBhuva没有。一切看起来都很好,在xcode中没有errorstry视图调试器在打开键盘后找出你的视图在哪里。你可以共享故事板视图层次结构的屏幕截图吗。我认为您的文本视图隐藏在其他视图后面。您在控制台上看到任何与约束相关的错误吗?@ParthBhuva No。一切看起来都很好,xcode中没有errorstry视图调试器。要在打开键盘后找出您的视图的位置,为了防止悬空的观察者,我建议添加Denit{}视图控制器的例程:NotificationCenter.default.removeObserverself为防止悬空的观察者,建议向视图控制器添加deinit{}例程:NotificationCenter.default.removeObserverself