2016-05-14 148 views
0

我有一個UITextView(purpose:comment)固定在我的屏幕底部,當用戶想添加評論時,鍵盤出現,並且評論視圖和註釋一起向上移動。我也有一個取消按鈕來隱藏鍵盤,但鍵盤不隱藏鍵盤沒有隱藏在ios中

//Set up NSNotification for Keyboard 
-(void) viewWillAppear:(BOOL)animated 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillToggle:) 
              name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillToggle:) 
              name:UIKeyboardWillHideNotification object:nil]; 
} 

//Code to shift comment view up with keyboard 
- (void) keyboardWillToggle:(NSNotification *)aNotification 
{ 
    CGRect frame = [self.navigationController.toolbar frame]; 
    CGRect keyboard = [[aNotification.userInfo valueForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue]; 
    frame.origin.y = keyboard.origin.y - frame.size.height; 
    [UIView animateWithDuration:[[aNotification.userInfo valueForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue] animations:^ 
    { 
    [self.navigationController.toolbar setFrame:frame]; 
}]; 
} 


//Hide keyboard 
-(void)cancelComment:(UIBarButtonItem*)sender{ 
    NSLog(@"cancelComment called"); 
    [self.view endEditing:YES]; 
} 

我覺得這應該工作?被記錄到控制檯 「cancelComment稱爲」,但鍵盤不能隱藏

+2

添加此[yourtextfield resignfirstresponder]在取消評論 –

+1

它在視圖控制器上有文本字段作爲子視圖? – Wain

+0

噢,就是這樣。><謝謝:) – abcf

回答

2

SOLUTION:

你忘了放:

[yourtextfield resignfirstresponder]; 

在cancelComment功能。

0
- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [yourtextfieldname resignfirstresponder]; 
} 

希望這會給你!

1

你可以嘗試

-(void)cancelComment:(UIBarButtonItem*)sender{ 
    NSLog(@"cancelComment called」); 
    [self.navigationController.view endEditing:YES]; 
} 

我覺得你在self.view並在self.navigationController.view的TextView不