0

我在视图控制器中添加了一个滚动视图并注册键盘显示和隐藏通知

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBar.translucent = NO;

    [self registerForKeyboardNotifications];

    // add UITapGestureRecognizer
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];
    [scrollView addGestureRecognizer:tap];
    scrollView.delegate = self;

    [self setupAView];
}

- (void)setupAView {
  // setting up aView...

  scrollView.contentSize = CGSizeMake(self.view.frame.size.width, aView.frame.size.height);

-(void)dismissKeyboard {
    [aView.aTextView resignFirstResponder];
}

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

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

}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, aView.frame.size.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    CGPoint aPoint = CGPointMake(0, aView.frame.size.height);
    [scrollView setContentOffset:aPoint animated:NO];
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    // scrollView insets
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

这很好用,当我点击 aTextView 时,scrollView 中的 aView 将移动到键盘视图上方,但同时如果我滚动 scrollView,整个视图会自动下降一点,这个动作怎么会改变 insets 值或 scrollView contentSize 值?

滚动视图下拉

4

0 回答 0