IOS swift3 键盘

ios swift3 键盘高度获取

1
2
3
4
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
//deinit 的时候remove 下

处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func keyboardWillShow(_ sender:Notification){
// 获取键盘高度
let userInfo = sender.userInfo
if userInfo == nil{
return
}
let value:NSValue = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
let keyBoardRect = value.cgRectValue
let height = keyBoardRect.size.height
}
func keyboardWillHide(_ sender:Notification){
// 隐藏了. 高度自然为0
}

oc 版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- (void)viewDidLoad
{
[super viewDidLoad];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{}

//键盘上面view 自动位置

1
2
NotificationCenter.default.addObserver(self, selector: #selector(self.keybordFrameChange(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keybordHid(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
func keybordHid(_ sender:Notification){
var info = (sender as NSNotification).userInfo //as? NSDictionary
let keyboardDuration = (info?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue ?? 0
self.inputDefaultView.mas_updateConstraints { (make) in
make?.bottom.mas_equalTo()(self.view)
}
UIView.animate(withDuration: keyboardDuration) {
self.view.layoutIfNeeded()
}
}
func keybordFrameChange(_ sender :Notification){
var keyHeight:CGFloat = 246
var info = (sender as NSNotification).userInfo //as? NSDictionary
let keyboardSize = (info?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue //UIKeyboardFrameEndUserInfoKey UIKeyboardFrameBeginUserInfoKey ,, begin的位置会有差距, 结束的最终的才是ok 的
let keyboardDuration = (info?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue ?? 0
if keyboardSize != nil{
keyHeight = (keyboardSize!.height)
}
self.inputDefaultView.mas_updateConstraints { (make) in
make?.bottom.mas_equalTo()(self.view)?.offset()(-keyHeight)
}
UIView.animate(withDuration: keyboardDuration) {
self.view.layoutIfNeeded()
}
}

//原本用这个 但是发现有时候位置不对, 换上面的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
func keyBoardWillShow(_ note:Notification)
{
if (note as NSNotification).userInfo == nil{
return
}
let userInfo = (note as NSNotification).userInfo! as NSDictionary
let keyBoardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
// let keyBoardBoundsRect = self.view.convertRect(keyBoardBounds, toView:nil)
// let keyBaoardViewFrame = inputeView.frame
let deltaY = keyBoardBounds.size.height
self.inputeDefaultView.isHidden = false
self.view.bringSubview(toFront: self.inputeDefaultView)
let animations:(() -> Void) = {
self.inputeDefaultView.transform = CGAffineTransform(translationX: 0,y: -deltaY)
}
if duration > 0 {
let options = UIViewAnimationOptions(rawValue: UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16))
UIView.animate(withDuration: duration, delay: 0, options:options, animations: animations, completion: nil)
}else{
animations()
}
}
func keyBoardWillHide(_ note:Notification)
{
if (note as NSNotification).userInfo == nil{
return
}
let userInfo = (note as NSNotification).userInfo! as NSDictionary
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let animations:(() -> Void) = {
self.inputeDefaultView.transform = CGAffineTransform.identity
}
if duration > 0 {
let options = UIViewAnimationOptions(rawValue: UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16))
UIView.animate(withDuration: duration, delay: 0, options:options, animations: animations, completion: nil)
UIView.animate(withDuration: duration, delay: 0, options: options, animations: animations, completion: { [weak self](bool) in
if let weakSelf = self{
weakSelf.inputeDefaultView.isHidden = true
}
})
}else{
animations()
}
}
Author

陈昭

Posted on

2016-10-27

Updated on

2021-12-27

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Kommentare

You forgot to set the shortname for Disqus. Please set it in _config.yml.