1

我有一个高度为 500 的 UIScrollView,UITextFileds 位于位置 0、100、200、300、400、500(1 个 UITextFileds、UITextFileds 2、3 个 UITextFileds、UITextFileds aUITextFileds 4 和 5)。

ContentOffset 位于 (0, 70) 位置。我点击UITextField 4,我怎么知道TextField 4 contentOffset的位置??

在此处输入图像描述

更新:

我点击了 UITextField 4,我怎么知道我必须向上 Y 像素,以便 UITextField 不会被键盘隐藏?

在此处输入图像描述

4

3 回答 3

1

我的代码:

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


}

-(void) viewWillAppear: (BOOL) animated {

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:)  name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{

}


- (void)setScrollView:(UIScrollView *)scrollView offsetToView:(UIView *)view
{

    CGRect contentFrame = CGRectMake(view.frame.origin.x, view.frame.origin.y,view.frame.size.width, view.frame.size.height);
    CGPoint contentOffset = CGPointMake(0, contentFrame.origin.y);

    scrollView.contentOffset = contentOffset;

}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self setScrollView:_scrolviewKeys offsetToView:textField];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    _scrolviewKeys.frame = (CGRect){
        _scrolviewKeys.frame.origin,(CGSize){
                _scrolviewKeys.frame.size.width,_scrolviewKeys.frame.size.height-kbSize.height}


    };
}

点击 UITextView 黄色

结果:

在此处输入图像描述

在此处输入图像描述

于 2013-10-22T12:31:32.423 回答
0

它应该很简单:

- (void)setScrollView:(UIScrollView *)scrollView bottomOffsetToView:(UIView *)view
{
   scrollView.contentOffset = CGPointMake(0, view.frame.origin.y + view.frame.size.height - 216);
}

然后在您的代码中将自己设置为 UITextFieldDelegate 并实现:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
     self.currentTextField = textField;
}

更新 :

你在这里问两个不同的事情。对于键盘通知的键盘注册(viewWillAppear例如):

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

然后根据键盘大小更改滚动视图框架:

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    self.scrollView.frame = (CGRect){self.scrollView.frame.origin,(CGSize){self.scrollView.frame.size.width,self.scrollView.frame.size.height-kbSize.height);
    if (self.currentTextField != nil)
    {
        [self setScrollView:self.scrollView bottomOffsetToView:self.currentTextField];
    }
}

keyboardWasHidden 的行为相同(重置初始帧),不要忘记删除 viewDidDisappear 中的 Observer。

于 2013-10-21T17:34:45.257 回答
0

在 iOS 7 中,scrollView 将自动滚动最小量以保持 textField 可见。如果您使用的是 iOS 6,那么您可以执行以下操作来模仿相同的行为。

假设您已调整scrollView.contentInset后键盘如下所示,这就是我所做的- (void)textFieldDidBeginEditing:(UITextField *)textField

self.scrollView.contentInset = (UIEdgeInsets){.bottom = 216.0f};
self.scrollView.scrollIndicatorInsets = self.scrollView.contentInset;

CGFloat minY = CGRectGetMinY([self.view convertRect:textField.frame fromView:textField]);
if (minY > self.scrollView.contentInset.bottom) {
    CGFloat visibleHeight = CGRectGetHeight(self.scrollView.bounds) - self.scrollView.contentInset.bottom;
    [self.scrollView setContentOffset:CGPointMake(0.0f, CGRectGetMaxY([self.view convertRect:textField.frame fromView:textField]) - visibleHeight) animated:YES];
}

我们首先根据scrollView的frame计算textField的frame的minY,然后看看它是否大于我们插入scrollView的量。如果是,那意味着 scrollView 的这一部分现在位于键盘下方。然后我们通过考虑 来计算偏移量visibleHeight,这是显示键盘后滚动视图的可见内容。取 textField 框架的 maxY 并减去 visibleHeight 以使其与键盘顶部对齐,从而将 scrollView 移动到使其可见的最小量。

于 2013-12-11T17:21:18.377 回答