UIKit:当子视图增加其超出屏幕边缘的宽度时,UIScrollView会自动滚动

Hoo*_*ing 13 iphone cocoa-touch objective-c uiscrollview uikit

在iPhone的背景下:

我有一个UIScrollView包含UIImage.当用户点击屏幕内部时UIImage,UITextField会在用户触摸的位置添加a .用户可以对此进行编辑UITextField,文本字段将根据是添加还是删除文本自动调整大小.

UITextField正在编辑的a 增加其宽度时,滚动视图会自动滚动以显示增加的宽度.

问题出在,因为文本字段的自动滚动不符合屏幕的y值

例如,假设用户在图像的底部添加了一个文本字段.当他们去编辑该文本字段时,键盘将显示,隐藏文本字段.我有代码来滚动屏幕以显示文本字段.当用户输入的文本太多以至于文本字段超出屏幕边缘时,就会出现问题.当这种情况发生时,屏幕水平滚动以适应更宽的文本,但也垂直 - 垂直滚动最终隐藏文本字段,基本上无效我做的任何事情来显示文本字段.

代码显示文本字段,如果它被键盘隐藏:

- (void)keyboardWasShown:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    self.offset = self.contentOffset;

    CGRect frame = self.frame;
    // self.activeField is the name of the field that is the current first responder - this just adds a little bit of padding
    frame.size.height -= keyboardSize.height + (self.activeField.frame.size.height * 2);

    if (!CGRectContainsPoint(frame, self.activeField.frame.origin)) {
        CGPoint scrollPoint = CGPointMake(self.offset.x, self.activeField.frame.origin.y - keyboardSize.height + (activeField.frame.size.height * 2));
    [self setContentOffset:scrollPoint animated:YES];
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是增加文本字段大小的代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    CGSize stringSize = [string sizeWithFont:textField.font];
    CGSize newSize = [newString sizeWithFont:textField.font];

    // Make textField wider if we're close to running up against it
    if (newSize.width > (textField.frame.size.width - self.widthOffset)) {
        CGRect textFieldFrame = textField.frame;
        if (stringSize.width > self.widthOffset) {
            textFieldFrame.size.width += stringSize.width;
        }
        textFieldFrame.size.width += self.widthOffset;
        textField.frame = textFieldFrame;
    }

    // Shrink the textField if there is too much wasted space
    if ((textField.frame.size.width - newSize.width) > self.widthOffset) {
        CGRect textFieldFrame = textField.frame;
        textFieldFrame.size.width = newSize.width + self.widthOffset;
        textField.frame = textFieldFrame;
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

问题是:UIScrollView在自动滚动时,如何让自己尊重自身的y值?

Tat*_*asi 5

基本上setFrameUIScrollView将调整滚动视图offset,通过做_adjustContentOffsetIfNecessary.由于该方法是私有的而没有记录,因此我们很难猜测调整将如何发生.有两种方法可以阻止不必要的滚动或offset设置错误:

1)UIScrollView offset应用后重置setFrame.如果您UIScrollView根据某些计算有意修改框架,则可以这样做.

CGPoint oldOffset = [scrollView contentOffset];         
scrollView.frame = CGRectMake(newFrame);
[scrollView setContentOffset:oldOffset];        
Run Code Online (Sandbox Code Playgroud)

2)应用offset没有动画的更改.在你的keyboardWasShown,改变 [self setContentOffset:scrollPoint animated:YES]; to
[self setContentOffset:scrollPoint animated:NO];

原因:offset启用多个动画时,结果offset不明确.这里内部方法(_adjustContentOffsetIfNecessary)应用偏移量变化,另一个由代码完成.如果您尝试记录UIScrollView委托方法中应用的所有偏移量,您可以注意到这一点:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@" offset: %@", NSStringFromCGPoint(scrollView.conentOffset))
}
Run Code Online (Sandbox Code Playgroud)

如果这有帮助,请告诉我.


jfo*_*cht 0

不要更改内容偏移量,而是更改滚动视图的显示框架。

- (void)keyboardWill:(NSNotification*)notification
{
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    // Update the scroll view's frame to the region not covered by the keyboard.
    self.frame = CGRectMake(self.fullSizedFrame.origin.x, 
                            self.fullSizedFrame.origin.y, 
                            self.fullSizedFrame.size.width, 
                            self.fullSizedFrame.size.height - keyboardSize.height);
}

- (void)keyboardWillHide:(NSNotification*)notification
{
    // Set the frame back to the original frame before the keyboard was shown.
    self.frame = self.fullSizedFrame;
}
Run Code Online (Sandbox Code Playgroud)

如果您不允许用户更改屏幕方向,则 fullSizedFrame 可以设置为视图首次显示时的原始框架。如果允许更改方向,您需要根据方向计算 fullSizedFrame 的适当值。