当我使用自定义textField.inputView时,UIKeyboardWillShowNotification不起作用

时间:2018-11-09 13:42:35

标签: ios objective-c uikeyboard

在我的程序中,我在xib上创建了一个自定义视图,并希望使用textField.inputView之类的视图。当KeyboardWillShow时,我想更改inputView框架,但是当我点击此textField时,UIKeyboardWillShowNotification不起作用。我如何更改textField.inputView或强迫工作UIKeyboardWillShowNotification

- (void)viewDidLoad {
    [super viewDidLoad]; 
    CGRect rect = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 0);
    self.actionView = [[ActionView alloc] initWithFrame:rect];            
    self.actionView.delegate = self; 
    self.currencyTextField.inputView = self.actionView;
    [self layout]; 
} 
- (void)addObserver{

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShown:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)keyboardWillShown:(NSNotification *)notification{
    NSDictionary* keyboardUserInfo = [notification userInfo];
    CGSize keyboardSize = [[keyboardUserInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    self.actionView.frame = CGRectMake(0, 0, keyboardSize.width, keyboardSize.height);
    [self.currencyTextField reloadInputViews];

}

1 个答案:

答案 0 :(得分:0)

您需要一个非零高度

CGRect rect = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 100);

也别忘了打电话

[self addObserver];

viewDidLoad的结尾

相关问题