我有一個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稱爲」,但鍵盤不能隱藏
添加此[yourtextfield resignfirstresponder]在取消評論 –
它在視圖控制器上有文本字段作爲子視圖? – Wain
噢,就是這樣。><謝謝:) – abcf