我知道我可以通过键盘上的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);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。