iOS iPhone – 在键盘上显示UIKeyboard高度

前端之家收集整理的这篇文章主要介绍了iOS iPhone – 在键盘上显示UIKeyboard高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道我可以通过键盘上的UIKeyboardFrameEndUserInfoKey获取UIKeyboard高度,并在它成为第一响应者时触发keyboardDidShow通知.

但是,我想知道在这些事件之前键盘的预期高度,所以我可以在视图控制器的viewDidLoad上设置某些设计元素.

由于设备正在改变,新的拼写校正条改变了键盘高度,我不想硬编码高度.

有没有人知道如何从键盘获得预期的高度考虑到它是否有自动纠正等?

解决方法

您可以通过以下方式完成:
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeTextView];

}
– (无效)initializeTextView {

// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidHide:)
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

myColoredTextview  = [[UITextView alloc]initWithFrame:CGRectMake(0,20,300,100)];
myColoredTextview.delegate = self;
[self.view addSubview:myColoredTextview];
myColoredTextview.backgroundColor = [UIColor lightGrayColor];
}

- (void)keyboardDidShow: (NSNotification *) notif{
// Do something here
 NSLog(@"show:%@",notif);
 NSDictionary *userInfo = [notif valueForKey:@"userInfo"];
 CGRect kbFrame = [[userInfo objectForKey:@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue];
 NSLog(@"keboardHeight:%f",kbFrame.size.height);

}

- (void)keyboardDidHide: (NSNotification *) notif{
// Do something here
NSLog(@"hide:%@",notif);

}

原文链接:https://www.f2er.com/iOS/333898.html

猜你在找的iOS相关文章