iPhone键盘与配件视图高度问题

7
我有一个带有输入附件视图的键盘,并使用keyboard-Will-Show通知来获取键盘高度。
问题在于第一次textfield成为第一响应者时,键盘返回216的高度(不包括附件视图的高度)。但是第二次聚焦于textfield时,返回的值是216 + 附件视图的高度。
如何仅获取键盘高度为216或216 + 附件视图的高度,以便根据它设置UI框架?
1个回答

1

我不确定你如何编写代码,但这是我为此编写的可用代码:

#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];


}

@longtranz 你在哪里使用了键盘高度?! - Mehrdad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接