【发布时间】:2017-05-03 21:28:59
【问题描述】:
我正在尝试创建这个帖子功能,它允许您插入文本和图像(类似于 iOS 设备上的 Note 应用程序)。但是,当我按下浅灰色区域(它是一个文本视图字段)时出现键盘时,我无法弄清楚如何移动或调整我的 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
}
}
这是控制器视图:
【问题讨论】:
标签: ios swift uiviewcontroller uiscrollview