2014-09-29 41 views
6

我有一個鍵盤與輸入附件視圖連接,我用鍵盤將顯示通知,以獲得鍵盤的高度。iPhone鍵盤與配件視圖高度問題

問題出在第一次textfield成爲第一響應者,鍵盤返回216高度(沒有輔助視圖的高度)。但是第二次關注文本框,價值回報是216 +附屬視圖的高度。

如何獲取鍵盤的高度只有216或216 +配件視圖的高度來設置基於它的UI框架?

回答

2

我不知道你是怎麼做的代碼,但這裏是我工作的代碼如下:

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


} 
+1

謝謝,它的工作就像一個魅力! – longtranz 2014-09-29 10:31:24