小编Oka*_*kan的帖子

键盘出现时调整屏幕大小

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

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)
    })
}
Run Code Online (Sandbox Code Playgroud)

但是,当我使用此代码时,第一条消息不显示.我想我必须调整tableview的大小.

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

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

我正在使用自动布局.

我该如何解决这个问题?

ios autolayout swift

17
推荐指数
2
解决办法
1万
查看次数

标签 统计

autolayout ×1

ios ×1

swift ×1