键盘出现时调整屏幕大小 [英] Resize the screen when keyboard appears

查看:123
本文介绍了键盘出现时调整屏幕大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个聊天应用。键盘出现时我必须移动文本字段。我使用以下代码执行此操作:

  func keyboardWillShow(notification:NSNotification){
if let userInfo = notification .userInfo {
如果让keyboardSize =(userInfo [UIKeyboardFrameBeginUserInfoKey]为?NSValue)?. CGRectValue(){
kbHeight = keyboardSize.height
self.animateTextField(true)
}
}
}
func keyboardWillHide(通知:NSNotification){
self.animateTextField(false)
}

func animateTextField(up: Bool){
var movement =(up?-kbHeight:kbHeight)

UIView.animateWithDuration(0.3,动画:{
self.view.frame = CGRectOffset(self.view) .frame,0,移动)
})
}

但是当我使用此代码,第一条消息不显示。我想我必须调整桌面视图的大小。



以下是截图之前之后键盘出现:



http://imgim.com/iossimulatorscreenshot7haz2015114958.png
http://imgim.com/iossimulatorscreenshot7haz2015115000.png



我正在使用自动布局。



如何解决此问题?

解决方案

2017



基于Eendje的概念.........



1)将KUIViewController复制并粘贴到您的项目中,



2)创建约束 - 它只是您内容的底部约束



3)不要忘记简单地将约束ID拖到 bottomConstraintForKeyboard



S. o你应该调整哪个视图?



注意,你可以使用 .view - 视图控制器的主视图 - 遗憾的是,您无法在iOS中调整 .view 的大小!



所以,只需将UIView命名为holder。



它位于 .view



将你的所有东西都放在持有人手中。



持有人当然会有四个简单的约束上/下/左/右 .view



对持有人的底部约束这里的班级确实是 bottomConstraintForKeyboard



(当然,通常情况下,当kayboard到达时,你想要一切压缩。在不寻常的情况下,你可能更喜欢让其他特定的东西向上移动,并且如果你想这样做,你可以在使用 KUIViewController 方法时有足够的灵活性。你可以将约束附加到你想要的任何东西。当然,几乎总是如此。它只是整个屏幕 - 因此只需要一个包含所有东西的持有者,并附加到那个。)



问题2 ...点击背景到清除键盘



通常在现代用户体验中,如果单击背景(所以:远离任何按钮,输入字段等),它应该关闭键盘。 / p>

KUIViewController 完全自动化了这种需求。你什么也不做,而且是自动的。

  class KUIViewController:UIViewController {

@IBOutlet var bottomConstraintForKeyboard:NSLayoutConstraint !

func keyboardWillShow(sender:NSNotification){
let i = sender.userInfo!
让k =(i [UIKeyboardFrameEndUserInfoKey]为!NSValue).cgRectValue.height
bottomConstraintForKeyboard.constant = k - bottomLayoutGuide.length
let s:TimeInterval =(i [UIKeyboardAnimationDurationUserInfoKey] as!NSNumber ).doubleValue
UIView.animate(withDuration:s){self.view.layoutIfNeeded()}
}

func keyboardWillHide(sender:NSNotification){
let info = sender.userInfo!
let s:TimeInterval =(info [UIKeyboardAnimationDurationUserInfoKey] as!NSNumber).doubleValue
bottomConstraintForKeyboard.constant = 0
UIView.animate(withDuration:s){self.view.layoutIfNeeded()}
}

func keyboardNotifications(){
NotificationCenter.default.addObserver(self,
selector:#selector(keyboardWillShow),
name:Notification。 Name.UIKeyboardWillShow,
object:nil)
NotificationCenter.default.addObserver(self,
selector:#selector(keyboardWillHide),
name:Notification.Name.UIKeyboardWillHide,
对象:nil)
}

func clearKeyboard(){
view.endEditing(true)
}

覆盖func viewDidLoad (){
super.viewDidLoad()
keyboardNotifications()
let t = UITapGestureRecognizer(target:self,action:#selector(clearKeybo) ard))
view.addGestureRecognizer(t)
t.cancelsTouchesInView = false
}
}

您可以



...在键盘可能出现的任何地方使用KUIViewController。



  class AddCustomer:KUIViewController,SomeProtocol {

class CustomerPicker:KUIViewController {

在这些屏幕上:


  1. 页面(你的。<$ c $当键盘在那里时,c> holder view)将自动调整大小以允许键盘。它会有很好的动画。


  2. 当用户点击关闭(点击背景)时,它会关闭键盘。







详情 - (不幸的是)点击您的内容也会解雇键盘。 (他们都得到了这个事件。)然而:确实,这几乎总是正常的Apple风格的正确行为:试一试。没有简单的方法可以避免这种情况。请务必阅读@iPruch答案中的示例。


I am building a chat app. I have to move a textfield when keyboard appears. I am doing this with the following code:

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardSize =  (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            kbHeight = keyboardSize.height
            self.animateTextField(true)
        }
    }
}
func keyboardWillHide(notification: NSNotification) {
    self.animateTextField(false)
}

func animateTextField(up: Bool) {
    var movement = (up ? -kbHeight : kbHeight)

    UIView.animateWithDuration(0.3, animations: {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement)
    })
}

But when I use this code, the first messages doesn't show. I guess I have to resize the tableview.

Here are screenshots Before and After the keyboard appears:

http://imgim.com/iossimulatorscreenshot7haz2015114958.png http://imgim.com/iossimulatorscreenshot7haz2015115000.png

I am using auto layout.

How can I resolve this problem?

解决方案

2017

Based on Eendje's concept .........

1) copy and paste KUIViewController in to your project,

2) create the constraint - it's simply your "bottom constraint" for your content

3) don't forget to simply drag the constraint id to bottomConstraintForKeyboard

So "which view should you resize?"

Note, you can not use .view - the main view of the view controller - unfortunately, you cannot resize the .view in iOS!

So, simply make a UIView named "holder".

It sits inside .view.

Put everything of yours inside holder.

Holder will of course have four simple constraints top/bottom/left/right to .view.

That bottom constraint to "holder" is indeed bottomConstraintForKeyboard in the class here.

(Of course, normally you want "everything" to squash up when the kayboard arrives. In unusual situations, you may prefer to have some other specific thing move up, and leave the rest alone. If you want to do that, you have total flexibility when using the KUIViewController approach. You can attach the constraint to whatever you want. Of course, almost always it's just "the whole screen" - hence simply have a "holder" which wraps everything, and attach to that.)

Issue 2... click 'background' to clear keyboard

Usually in modern UX, if you click in the "background" (so: away from any buttons, input fields, etc) it should dismiss the keyboard.

The KUIViewController class completely automates that need. You do nothing and it's automatic.

class KUIViewController: UIViewController {

    @IBOutlet var bottomConstraintForKeyboard: NSLayoutConstraint!

    func keyboardWillShow(sender: NSNotification) {
        let i = sender.userInfo!
        let k = (i[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
        bottomConstraintForKeyboard.constant = k - bottomLayoutGuide.length
        let s: TimeInterval = (i[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
        UIView.animate(withDuration: s) { self.view.layoutIfNeeded() }
    }

    func keyboardWillHide(sender: NSNotification) {
        let info = sender.userInfo!
        let s: TimeInterval = (info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
        bottomConstraintForKeyboard.constant = 0
        UIView.animate(withDuration: s) { self.view.layoutIfNeeded() }
    }

    func keyboardNotifications() {
        NotificationCenter.default.addObserver(self,
            selector: #selector(keyboardWillShow),
            name: Notification.Name.UIKeyboardWillShow,
            object: nil)
        NotificationCenter.default.addObserver(self,
            selector: #selector(keyboardWillHide),
            name: Notification.Name.UIKeyboardWillHide,
            object: nil)
    }

    func clearKeyboard() {
        view.endEditing(true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        keyboardNotifications()
        let t = UITapGestureRecognizer(target: self, action: #selector(clearKeyboard))
        view.addGestureRecognizer(t)
        t.cancelsTouchesInView = false
    }
}

You can

... use KUIViewController anywhere a keyboard might appear.

class AddCustomer: KUIViewController, SomeProtocol {

class CustomerPicker: KUIViewController {

On those screens:

  1. The page (your .holder view) will resize automatically to allow for the keyboard, when a keyboard is there. It will animate nicely.

  2. When the user clicks "off" (clicks on the "background"), it will dismiss the keyboard.


A detail - (unfortunately) clicks on your content will also dismiss the keyboard. (They both get the event.) However: indeed, this is almost always correct behavior in the usual Apple style: give it a try. There is no simple way to avoid this. Be sure to read @iPruch answer for an example.

这篇关于键盘出现时调整屏幕大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆