其实监听键盘。大家通常的做法。都是注册通知观察者,从通知的内容中取得键盘的size。然后在对需要调整的view修改他们frame,或者transform.
在研究他们的代码后,我采用根控制器的方法。来解决键盘的监听。在需要监听键盘的地方,只需要给
KeyWordview赋值即可。一行代码解决了键盘的监听,
class BaseViewController: UIViewController {
var KH:CGFloat = 0
var KeyWordview: UIView?{
didSet{
KH = (KeyWordview?.frame.origin.y)!
}
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(note:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(note:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
}
@objc func keyboardWillShow(note: NSNotification) {
let name:String = note.name.rawValue
print(name)
let userInfo = note.userInfo!
let keyBoardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let deltaY = keyBoardBounds.size.height
//获取键盘的size
let kbRect = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
if name == "UIKeyboardDidShowNotification" {
//弹出键盘执行的方法。
let options = UIViewAnimationOptions(rawValue: UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16))
UIView.animate(withDuration: 0.5) {
self.KeyWordview?.frame.origin.y = self.KH - deltaY
}
}else if (name == "UIKeyboardDidHideNotification"){
//收起键盘的操作
UIView.animate(withDuration: 0.5) {
self.KeyWordview?.frame.origin.y = self.KH
}
}else{
}
}
}
下面讲一下怎么使用。
import UIKit
let homeH = 185+tabBarbottomHeight+44+10+10;
class HomeViewController: BaseViewController,AMapLocationManagerDelegate {
let defaultLocationTimeout = 6
let defaultReGeocodeTimeout = 3
var mapView: MAMapView!
var displayLabel: UILabel!
var addressBlock:PassBak?
var poi:AMapAOI?
var completionBlock: AMapLocatingCompletionBlock!
var footView:HomeFootView = {
return HomeFootView.LoadView()
}()
lazy var locationManager = AMapLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
//self.view.backgroundColor = UIColor.red;
configLocationManager()
setNavgationBar();
initMapView()
locationManager.startUpdatingLocation();
}
}
extension HomeViewController{
func initMapView() {
mapView = MAMapView(frame: CGRect.init(x: 0, y: 0, width: KScreenW, height: KScreenH-185))
mapView.delegate = self
mapView.showsCompass = true
mapView.isShowsUserLocation = true
view.addSubview(mapView)
view.addSubview(footView)
footView.frame = CGRect.init(x: 0, y: KScreenH-185-tabBarbottomHeight-44-10-10,width: KScreenW, height:homeH )
footView.operationBlock = {(methodType,str,closureblock)
in
self.addressBlock = closureblock
switch methodType {
case .address:do {
let vc = AddressViewController()
vc.callBack = { poi
in
self.addressBlock!(poi.name)
}
UIView.animate(withDuration: 0.5, animations: {
self.view.frame.origin.y = self.view.frame.origin.y - 300
self.present(vc, animated: true, completion: nil)
})
}
break
default: break
}
}
//所需要的一行代码就在这儿,给需要作出改变的view。赋值给keyWordView.就可以了。
self.KeyWordview = footView
}