【问题标题】:iPhone Keyboard with accessory view height problems带有附件视图高度问题的 iPhone 键盘
【发布时间】:2014-11-23 15:08:18
【问题描述】:

我有一个附加了输入附件视图的键盘,我使用了键盘将显示通知来获取键盘的高度。

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

如何获取仅 216 或 216 + 辅助视图高度的键盘高度来设置 UI 框架基础?

【问题讨论】:

    标签: ios objective-c iphone keyboard


    【解决方案1】:

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

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

    【讨论】:

    • @longtranz 你在哪里用的键盘高度?!
    猜你喜欢
    • 1970-01-01
    • 2018-05-17
    • 2011-08-15
    • 2016-10-21
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 2018-03-07
    • 2011-10-31
    相关资源
    最近更新 更多