简体   繁体   English

出现键盘时如何调整ScrollView的大小?

[英]How to resize ScrollView when keyboard appears?

I'm trying to create this post feature which would allow you to insert text as well as images (similar to Note app on iOS devices). 我正在尝试创建此发布功能,该功能将允许您插入文本和图像(类似于iOS设备上的Note应用)。 However, I can't figure out how to move or resize my ScrollView when the keyboard appears when I press on the light-grey area (it's a text view field), so the grey area would resize and the add button would move above the keyboard when it appears. 但是,当我在浅灰色区域(这是一个文本视图字段)上按键盘时,我不知道如何移动或调整ScrollView的大小,因此灰色区域将调整大小,并且添加按钮将移动到键盘出现时。

//  PostViewController.swift
//
//  Created by Martynas on 09/12/2016.
//  Copyright © 2016 Martynas. All rights reserved.
//

import UIKit
import Firebase

class PostViewController: UIViewController, UITextFieldDelegate {

@IBOutlet var ScrollView: UIScrollView!
@IBOutlet var titleTextField: UITextField!
@IBOutlet var contentTextField: UITextView!
@IBOutlet var Menu: UIView!

@IBAction func hideKeyboardWhenSwippedDown(_ sender: Any) {
    contentTextField.endEditing(true)
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Hide keyboard when...
    self.hideKeyboardWhenTappedAround() // ...press anywhere outside the keyboard

    self.titleTextField.delegate = self
    self.contentTextField.delegate = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func sendTapped(_ sender: Any) {

    if let uid = FIRAuth.auth()?.currentUser?.uid {
        if let title = titleTextField.text {
            if let content = contentTextField.text {
                let postObject: Dictionary<String, Any> = [
                    "uid": uid,
                    "title": title,
                    "content": content
                ]

                FIRDatabase.database().reference().child("posts").childByAutoId().setValue(postObject)

            }
        }
    }
}

@IBAction func addTapped(_ sender: Any) {

}

func textFieldDidBeginEditing(_ textField: UITextField) {
    if textField == contentTextField {
        ScrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
    } else {
        return
    }

}

func textFieldDidEndEditing(_ textField: UITextField) {
    if textField == contentTextField {
        ScrollView.setContentOffset(CGPoint(x: 0, y: 250), animated: true)
    } else {
        return
    }
}

// Hide keyboard when user presses 'return' key on the keyboard...
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    titleTextField.resignFirstResponder()
    return true
    }
}

This is the Controller View: 这是控制器视图:

控制器视图

You need to listen to the keyboardDidShow/Hide notification and adjust the scroll view's height accordingly. 您需要收听keyboardDidShow/Hide通知并相应地调整滚动视图的高度。 The keyboardDidShow notification userInfo contains the keyboard's frame and therefore height. keyboardDidShow通知userInfo包含键盘的框架以及高度。

Presuming your scroll view has a constraint at the bottom of the super view, you can make it an IBOutlet and animate it's constant to the height of the keyboard and back to 0 when the keyboard show and hide notifications are fired respectively. 假设滚动视图在超级视图的底部具有约束,则可以使其成为IBOutlet并对其进行动画处理,使其恒定于键盘的高度,并在分别触发键盘显示和隐藏通知时使其返回0。

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

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