UIEdgeInsets的工作方式与预期不完全相同

时间:2014-10-02 07:00:50

标签: ios objective-c uiscrollview uiedgeinsets

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

- (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值?

scrollview drops down

0 个答案:

没有答案