ios - 具有附件 View 高度问题的 iPhone 键盘

标签 ios objective-c iphone keyboard

我有一个附有输入附件 View 的键盘,我使用 keyboard-Will-Show 通知来获取键盘的高度。

问题是第一次文本字段成为第一响应者时,键盘返回 216 的高度(没有附件 View 的高度)。但是第二次关注textfield,返回值是216 + accessory view的高度。

如何获取键盘高度只有 216 或 216 + 附件 View 的高度来设置基于它的 UI 框架?

最佳答案

我不确定你是如何编写代码的,但这是我的工作代码:

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


}

关于ios - 具有附件 View 高度问题的 iPhone 键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26096253/

相关文章:

c# - Xamarin 应用程序在模拟器上构建和运行,但在真实手机上我遇到装配失败

iphone - 我如何在 UIView 手势下检测 UIButton 的 TouchUp 事件

ios - 如何使用 RestKit Testing 测试此对象映射

iphone - iOS 复制 SQLite 数据库并将其从 iCloud 中排除

iphone - 从设备中删除应用程序后本地通知仍然存在

android - 多个应用程序可以处理 ibeacon 事件吗

ios - Xamarin.iOS 中的等效 CGPointZero 是什么?

ios - 如何将适用于 iOS 的 AWS 开发工具包与 Objective-C 而不是 Swift 一起使用?

iphone - 以 Objective C 的方式编写 for 循环

ios - iPhone API 获取本地设备电话号码