我正在使用底部有文本字段的tableview控制器。当它运行并且我单击文本字段时,整个页面滚动到底部,包括位于顶部的导航栏。我想要导航baR FIXED ON TOP并且表格视图的滚动位置保持不变。你能告诉我另一种改变tableview框架或其他方法的方法吗?我目前正在使用iOS 11.0进行测试,Xcode 9.1 swift 4
import UIKit
import GRDB
import IQKeyboardManagerSwift
import Alamofire
import SVProgressHUD
import UIEmptyState
import SwiftyJSON
class AssistantController: UIViewController , UITableViewDelegate , UITableViewDataSource , UITextFieldDelegate , UIEmptyStateDelegate , UIEmptyStateDataSource {
var user : User!
var assist : [Assistant] = []
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var txtEmail : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView.init()
let btn2 = UIButton(type: .custom)
btn2.setImage(UIImage(named: "back_arrow"), for: .normal)
btn2.tintColor = UIColor.init(red: 219.0/255.0, green: 103.0/255.0, blue: 93.0/255.0, alpha: 1)
btn2.frame = CGRect(x: 0, y: 0, width: 45, height: 45)
btn2.addTarget(self, action: #selector(compopen(sender:)), for: .touchUpInside)
let item2 : UIBarButtonItem = UIBarButtonItem(customView: btn2)
self.navigationItem.setLeftBarButton(item2, animated: true)
self.navigationItem.title = "AssistMan".localized()
self.navigationController?.setNavigationBarHidden(false, animated: true)
txtEmail.placeholder = "Assistant Email (Optional)".localized()
IQKeyboardManager.sharedManager().enableAutoToolbar = false
txtEmail.delegate = self
//txtEmail.becomeFirstResponder()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
//tableView.isScrollEnabled = false
}
@objc func keyboardWillShow(_ notification:Notification) {
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
if keyboardFrame.size.height <= 0 { // to fix bug on iOS 11
keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
}
keyboardFrame = self.view.convert(keyboardFrame, from: nil)
tableView.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - keyboardFrame.height)
}
@objc func keyboardWillHide(_ notification:Notification) {
tableView.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
IQKeyboardManager.sharedManager().enableAutoToolbar = true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool { // called when 'return' key pressed. return {
print("typed")
textField.resignFirstResponder()
// add
//executeInsert ()
return true
}
func executeInsert () {
self.view.endEditing(true)
let assistantEmail : String = txtEmail.text!
if assistantEmail.isEmpty {
SVProgressHUD.showError(withStatus: "Please type your email".localized())
SVProgressHUD.dismiss(withDelay: 2.0)
return
}
}
@IBAction func executeAdd(_ sender: Any) {
executeInsert ()
}
@objc func compopen ( sender: UIBarButtonItem ) {
self.navigationController?.popViewController(animated: true)
}
override func viewWillAppear(_ animated: Bool) {
user = User.sharedInstance
enqueue(subUrl: "GetAssistant", inputDict: user.getAssist())
getData()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if assist.isEmpty {
self.reloadEmptyStateForTableView(self.tableView)
//self.emptyStateView?.tintColor = UIColor.themeRed()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "刪除".localized()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return assist.count
}
}