UIKit: UIScrollView automatically scrolling when a subview increases its width past the edge of the screen
就iPhone而言:
我有一个
当正在编辑的a
问题出现是因为文本字段的自动滚动不尊重屏幕的Y值。
例如,假设用户在图像底部添加了一个文本字段。当他们编辑文本字段时,键盘将显示,隐藏文本字段。我有代码可以滚动屏幕显示文本字段。当用户输入的文本太多以至于文本字段超出屏幕边缘时,问题就出现了。当发生这种情况时,屏幕水平滚动以适应更宽的文本,但也垂直滚动-垂直滚动最终隐藏了文本字段,基本上取消了我显示文本字段所做的任何操作。
如果文本字段被键盘隐藏,则显示该字段的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | - (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]; } } |
下面是增加文本字段大小的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | - (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; } |
问题是:当自动滚动时,如何让
基本上,
1)应用
1 2 3 | CGPoint oldOffset = [scrollView contentOffset]; scrollView.frame = CGRectMake(newFrame); [scrollView setContentOffset:oldOffset]; |
2)无动画应用
[self setContentOffset:scrollPoint animated:NO];
原因:当动画打开时应用多个
1 2 3 4 | -(void)scrollViewDidScroll:(UIScrollView *)scrollView { NSLog(@" offset: %@", NSStringFromCGPoint(scrollView.conentOffset)) } |
如果有帮助,请告诉我。
一种可能的解决方法是响应
如果只有当
不要更改内容偏移量,而是更改滚动视图的显示框。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | - (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; } |
如果不允许用户更改屏幕方向,则当视图首次显示时,可以将fullsizedframe设置为视图的原始帧。如果允许更改方向,则需要根据方向为FullSizedFrame计算适当的值。