简体   繁体   English

iOS中的键盘处理

[英]Keyboard handling in iOS

I have an issue with my application. 我的申请有问题。 I am actually building a login page and I do not find how to handle the keyboard when a text field is active. 我实际上是在建立一个登录页面,当文本字段处于活动状态时,我找不到如何处理键盘。 I have two text fields in a view. 我在视图中有两个文本字段。 In my superView, I have 3 views. 在我的superView中,我有3个视图。 One on the bottom, another on the middle and a last on top. 一个在底部,另一个在中间,最后一个在顶部。 The 3 views are embeded in a stack view. 这3个视图被嵌入到堆栈视图中。

I really want to shrink the stack view, in other words reduce the space between all 3 views only when the keyboard appears and set the size back when the keybaord disappear, like Facebook login page on iOS. 我真的想缩小堆栈视图,换句话说,仅当键盘出现时才减小所有3个视图之间的空间,并在键盘消失时设置大小,如iOS上的Facebook登录页面。

You need to subscribe to keyboard notifications and do this in viewDidLoad 您需要订阅键盘通知,并在viewDidLoad中进行操作

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil)

// //

@objc func keyboardWillShow(notification: NSNotification) {
   self.stackV.spacing = 10
    UIView.animate(withDuration: 0.3, animations: { () -> Void in
        self.view.layoutIfNeeded()
    })
}

@objc func keyboardWillHide(notification: NSNotification) {
    self.stackV.spacing = // set back to default
    UIView.animate(withDuration: 0.3, animations: { () -> Void in
        self.view.layoutIfNeeded()
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM