【问题标题】:UIKeyboardWillShowNotification not working on iOS 7UIKeyboardWillShowNotification 在 iOS 7 上不起作用
【发布时间】:2016-09-14 20:20:58
【问题描述】:

UIKeyboard 将显示和隐藏通知方法在 iOS8 及更高版本中可以正常工作,但在 iOS7 中无法正常工作。有没有其他选择?

我的应用部署目标是 iOS7。

我的代码在这里

- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

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

- (void)keyboardWasShown:(NSNotification *)sender
{
    CGSize kbSize =
        [[[sender userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    if (!scroll) {
        scrollValue = self.WholeScreenUIView.frame.origin.y - kbSize.height;
    }

    scroll = YES;
}

- (void)HideKeyboard:(NSNotification *)sender
{

    scroll = NO;

    scrollValue = 0.0;
}

提前致谢。

【问题讨论】:

  • 能不能详细说明一下?该方法是否在 iOS 7 中被调用?

标签: objective-c cocoa-touch ios7 nsnotificationcenter uikeyboard


【解决方案1】:

使用此代码..

 [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(keyboardWillHideHandler:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillshowHandler:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

- (void) keyboardWillHideHandler:(NSNotification *)notification {
   [scroll setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void) keyboardWillshowHandler:(NSNotification *)notification {
    [scroll setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+44)];

}

希望这会有所帮助。这适用于我的情况

【讨论】:

  • 这段代码和问题没有什么不同,只是你注册的观察者代码没有放在任何方法中。
  • 感谢您的支持,以上代码在 iOS 8 中有效,但在 iOS 7 中无效。仅供参考:在 iOS 7 中未调用观察者函数
猜你喜欢
  • 2018-03-07
  • 2018-02-05
  • 2013-10-19
  • 1970-01-01
  • 2013-10-19
  • 2013-09-16
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多