ios - 键盘未隐藏在 ios 中

标签 ios objective-c

我在屏幕底部固定了一个 UITextView(用途:评论),当用户想要添加评论时,键盘出现,我让评论 View 随着评论一起向上移动。我还有一个隐藏键盘的取消按钮,但键盘没有隐藏

//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 called”正在记录到控制台,但键盘未隐藏

最佳答案

解决方案:

你忘了放:

[yourtextfield resignfirstresponder];

在您的 cancelComment 函数中。

关于ios - 键盘未隐藏在 ios 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37226887/

相关文章:

objective-c - 如何获取PubNub单个未读消息的数量?

ios - OpenCV for iOS 框架错误 "Base.hpp"header must be compiled as C++

iphone - 使用 IP 地址、端口和凭据进行实时视频流监控

ios - 未找到 <TargetName-Swift.h> 文件

ios - 从另一个 ViewController 呈现 TabBarController

ios - UIWebView复用

ios - 如何在 sks 文件 "SpriteKit action"中的 Xcode Action 编辑器上获取所有 Action 名称

ios - WatchKit 错误 - 无法找到要实例化的接口(interface) Controller 类 ''

ios - 如果 UIImage 设置为 .scaleAspectFill,它会与标签重叠

objective-c - 使 Web View 中的所有链接在默认浏览器中打开