- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 iPhone 的上下文中:
我有一个包含 UIImage
的 UIScrollView
。当用户点击 UIImage
内的屏幕时,将在用户触摸的位置添加一个 UITextField
。用户可以编辑此 UITextField
,文本字段将根据添加或删除文本自动调整大小。
当正在编辑的 UITextField
增加其宽度时, ScrollView 会自动滚动以显示增加的宽度。
出现问题是因为文本字段的自动滚动不符合屏幕的 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];
}
}
这是增加文本字段大小的代码:
- (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;
}
问题是:如何让 UIScrollView
在自动滚动时遵守自身的 y 值?
最佳答案
基本上setFrame
的 UIScrollView
将重新调整 ScrollView offset
,由 _adjustContentOffsetIfNecessary
完成.由于该方法是私有(private)的且没有记录,因此我们几乎无法猜测调整将如何发生。有两种方法可以停止不必要的滚动或错误 offset
正在设置:
1) 重置UIScrollView
offset
申请后setFrame
.如果您正在修改 UIScrollView
的框架,您可以这样做有意地基于一些计算。
CGPoint oldOffset = [scrollView contentOffset];
scrollView.frame = CGRectMake(newFrame);
[scrollView setContentOffset:oldOffset];
2) 申请offset
没有动画的变化。在你的keyboardWasShown
, 改变 [self setContentOffset:scrollPoint animated:YES]; to<br/>
[self setContentOffset:scrollPoint animated:NO];
原因:当不止一个offset
应用了动画,结果 offset
是模棱两可的。这里的内部方法 ( _adjustContentOffsetIfNecessary
) 应用偏移量更改,另一个由您的代码完成。如果您尝试记录在 UIScrollView
中应用的所有偏移量,您会注意到这一点。委托(delegate)方法:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@" offset: %@", NSStringFromCGPoint(scrollView.conentOffset))
}
如果这有帮助,请告诉我。
关于iphone - UIKit:当 subview 增加其宽度超过屏幕边缘时,UIScrollView 自动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6238031/
抱歉这个奇怪的标题,但是,这正是我所需要的。我知道这是可以做到的,因为我见过其他应用程序这样做。 基本上,我的应用程序有一个启用分页的大 ScrollView ,其中包含一些嵌套的 ScrollVie
我还没有尝试过,但我假设一旦我知道用户想要在子 ScrollView 中滚动,我就必须禁用父 ScrollView 的滚动,对吗? 两个 ScrollView 都是水平滚动的。 如何暂时禁用父级的滚动
我需要实现这个方法: - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center { 但我不知道如何获得我需要的中心坐标。通常
我想根据 UIAccelerometer 值滚动 ScrollView 。实现这一目标的最佳方法是什么? 最初,我根据获得的加速度计值将内容偏移设置为一个值。然后在每个 scrollViewDidEn
我有一个 UIScrollView,我需要底部淡入透明,这样它就不会突然切断内容。 UIScrollView 的背景是自定义颜色。这是我到目前为止所拥有的,但图层显示为白色,而不是自定义颜色。这就是我
我有一个 UIScrollviews 彼此存在的层次结构。 如何将特定区域的滑动事件重定向到内部 ScrollView ? 最佳答案 如果您将 UIScrollViews 设置为只能在一个轴或另一轴上
在我的应用程序中,我有一个扩展 UIScrollView 并在用户滚动时填充其内容的 View 。但是,如果用户滚动太快,则 UIScrollView 内填充的 View 不会及时创建,您实际上可以看
我有一个包含内容的 UIScroll View 。如果该内容溢出,使 ScrollView 可滚动,我想将 View 的底部设置为某种颜色。如果没有,我想将其设置为不同的颜色。 我的问题是,我不知道如
我有一个 ScrollView ,其中包含一个 UIImageView,以及一个额外的 UIScrollView 和 UIImageView。 我想要实现的是独立缩放和平移每个 ScrollView
类似的问题也有人问过这个,但我的困境其实和他们正好相反。我有一个全屏、分页 UIScrollView,每个页面都是屏幕的大小。在其中,我有一些页面本身就是 UIScrollViews,它们的宽度大于屏
我有一个 UIScrollView 和另一个 UIScrollView 。它们都水平滚动并且具有 pagingEnabled = YES。假设我开始滚动内部 ScrollView 并到达最右边界。如果
我有一个 UIScrollView,里面有一个(自定义)UIView。 在我的 scrollViewDidScroll 方法中,我正在调用 [myCustomView setNeedsDisplay]
我有一个水平滚动的 UICollectionView,其中填充了垂直滚动的 UITableViews(两者都是 UISCrollView 的子类)。当滚动手势开始沿任一方向滚动时,在其减速完成之前不会
iOS 8, swift 。我试图在水平 ScrollView 上创建垂直 ScrollView 。它们都是 UIScrollView。垂直 ScrollView 允许从底部向上滑动 View 。顶部
我需要一个分页的 UIScrollView,它以 UIScreenEdgePanGestureRecognizer 的方式检测屏幕边缘的平移,而不是像 UIPanGestureRecognizer 那
假设我有多个 ScrollView ,并且我希望在任何给定时间只打开其中一个 (scrollView.contentOffset != 0)。因此,当 scroll view 1 打开且 scroll
我在分页 UIScrollView(外部)中的 VC 中有 UIScrollView(内部 - UICollectionView 是诚实的)。两者都是水平的。当我向内滚动到边缘(左侧或右侧)时,它开始
如何在不干扰现有 UIScrollView 的情况下从现有 UIScrollView 获取重复的 UIScrollView。 第一个 ScrollView , UIScrollView *firstS
我的应用程序中有一个点,我删除了一个 ScrollView 并添加了另一个。我希望新 ScrollView 中也能出现上一个 ScrollView 中的相同手势。我尝试过: s
请告诉我如何在类似嵌套进程的 ScrollView 中设置 ScrollView 。 以下代码部分有效。 int x=10; int y=10; for(int i=0; i<5; i++) {
我是一名优秀的程序员,十分优秀!