我有一个附有输入附件 View 的键盘,我使用 keyboard-Will-Show 通知来获取键盘的高度。
问题是第一次文本字段成为第一响应者时,键盘返回 216 的高度(没有附件 View 的高度)。但是第二次关注textfield,返回值是216 + accessory view的高度。
如何获取键盘高度只有 216 或 216 + 附件 View 的高度来设置基于它的 UI 框架?
最佳答案
我不确定你是如何编写代码的,但这是我的工作代码:
#pragma mark - Keyboard Notification
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
NSValue *keyBoardEndFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [keyBoardEndFrame CGRectValue].size;
self.keyboardSize = keyboardSize;
}
- (void)keyboardWillHide:(NSNotification *)notification {
self.keyboardSize = CGSizeZero;
}
- (void) addToolBarToTextView {
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.barStyle = UIBarStyleBlack;
toolBar.translucent = YES;
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneBtn setFrame:CGRectMake(0, 7, 65, 30)];
doneBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
[doneBtn setTitle:@"Next" forState:UIControlStateNormal];
[doneBtn addTarget:self action:@selector(keyBoardDoneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * barItem = [[UIBarButtonItem alloc] initWithCustomView:doneBtn];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
toolBar.items = [NSArray arrayWithObjects: flexibleSpace, barItem, nil];
mobileTxtField.inputAccessoryView = toolBar;
}
-(void)viewDidLoad {
[super viewDidLoad];
[self addToolBarToTextView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
关于ios - 具有附件 View 高度问题的 iPhone 键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26096253/