我知道我可以通过keyboardWillShow上的UIKeyboardFrameEndUserInfoKey获取UIKeyboard高度,并在它成为第一响应者时触发keyboardDidShow通知。
但是,我想知道这些事件之前键盘的预期高度,以便我可以在视图控制器的viewDidLoad中设置某些设计元素。
由于设备正在改变并且新的拼写校正条改变了键盘高度,我不想硬编码高度。
有没有人知道如何从键盘获得预期的高度,考虑到它是否有自动纠正等?
答案 0 :(得分:0)
您可以通过以下方式进行:
- (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);
}