我知道我可以通过键盘上的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);
}