繁体   English   中英

在自动布局的scrollview setContentOffset无法正常工作

[英]In auto-layout scrollview setContentOffset not working correctly

我正在使用全自动布局。

最后一个UITextField (在完成按钮之前视图的结尾):我在textFieldDidBeginEditing设置了setContentOffset ,它工作正常,但是当我尝试滚动(在显示的键盘上)时, UIScrollView恢复了正常。

然后将文本字段返回键盘。 这是我在textFieldDidBeginEditing代码。

我正在使用bskeyboardcontroller作为下一个和上一个以及完成按钮。

[self.scrollviewDetail setContentOffset:CGPointMake(0, 500) animated:YES];

问题是您在视图中键盘面板外观上的滚动视图高度,只需更改滚动昆虫或框架即可避免此问题,如果滚动视图的内容偏移大于(内容大小-滚动视图框架大小),而滚动时它将在滚动视图中自动重置。

试试这个代码:

  - (void)viewWillAppear:(BOOL)animated {

        [super viewWillAppear:animated];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
    }

    - (void)viewWillDisappear:(BOOL)animated {

        [super viewWillDisappear:animated];
        [[NSNotificationCenter defaultCenter] removeObserver:self];

    }

    - (void)keyboardDidShow:(NSNotification *)notification {

        CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardRect.size.height, 0.0);
        scrollView.contentInset = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;

CGRect aRect = self.view.frame;
    aRect.size.height -= keyboardRect.size.height;
    if (!CGRectContainsPoint(aRect, currentTextField.frame.origin) ) {
        [scrollView scrollRectToVisible:currentTextField.frame animated:NO];
    }
    }
    - (void)keyboardDidHide:(NSNotification *)notification
    {

        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        scrollView.contentInset = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM