【发布时间】:2014-01-08 10:15:54
【问题描述】:
我正在尝试使用嵌套的UIScrollView 实现分页解决方案,但遇到了一个烦人的问题,很难解决。
视图加载得很好,但一旦用户启动滚动,视图就会从顶部弹出大约 20 像素,并且在下拉时会弹回该位置。
我已遵循其他已回答问题中的指南,但似乎无法确定问题所在。
当我单独使用 innerScroll 时,垂直效果很好。
我一嵌套它,问题就出现了。
我尝试将contentSize 的高度增加到超过内容的高度,因为我猜这会是问题,但似乎没有什么不同。
CGRect screenSize = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
_theScrollView=[[customScrollView alloc] initWithFrame:screenSize];
_theScrollView.pagingEnabled = YES;
_theScrollView.directionalLockEnabled = YES;
_theScrollView.bounces = NO;
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(performAction:)];
NSArray* buttons = [[NSArray alloc]initWithObjects:barButton, nil];
CGSize scrollViewContentSize = screenSize.size;
scrollViewContentSize.width = screenSize.size.width * self.thisform.formPages.count;
scrollViewContentSize.height = self.view.bounds.size.height;
_theScrollView.contentSize = scrollViewContentSize;
self.actionButton = barButton;
self.picVisible = NO;
self.navigationItem.rightBarButtonItems = buttons;
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(onCancelButtonSelected:)];
self.navigationItem.leftBarButtonItem = backButton;
[self.view setUserInteractionEnabled:YES];
self.views = [[NSMutableArray alloc] initWithCapacity:_thisform.formPages.count];
int i = 0;
NSSortDescriptor *byPage = [[NSSortDescriptor alloc] initWithKey:@"formPageNumber"
ascending:YES];
NSArray *sortedPages = [self.thisform.formPages sortedArrayUsingDescriptors:[NSArray arrayWithObjects: byPage, nil]];
float zoomScale=1.0;
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
zoomScale=1.0;
} else {
zoomScale=1.3;
}
BOOL isNew;
for (FormPages *page in sortedPages) {
NSData *formImage = page.formPage;
//innerScroll
UIImage *image = [UIImage imageWithData:formImage];
Imager *imageView = [[Imager alloc] initWithImage:image];
[imageView setUserInteractionEnabled:YES];
imageView.tag = page.formPageNumber;
imageView.fieldCollection = [Utility populateFormFields:self.thisform
pagenumber:page.formPageNumber];
CGRect frame = CGRectMake(_theScrollView.bounds.size.width * i, 0, _theScrollView.bounds.size.width, _theScrollView.bounds.size.height);
float contentWidth = _theScrollView.bounds.size.width;
float contentHeight = imageView.frame.size.height;
CGSize contentSize = CGSizeMake (contentWidth,contentHeight);
TPKeyboardAvoidingScrollView *innerScroll = [[TPKeyboardAvoidingScrollView alloc] initWithFrame:frame];
innerScroll.contentSize = contentSize;
[innerScroll setUserInteractionEnabled:YES];
innerScroll.minimumZoomScale=1;
innerScroll.zoomScale = zoomScale;
innerScroll.maximumZoomScale=2.5;
innerScroll.delegate = self;
innerScroll.scrollsToTop=NO;
innerScroll.currentView = imageView;
imageView.frame = screenSize;
CGSize pageSize = [Utility GetPageSize:self.thisform];
float viewWidth = imageView.frame.size.width;
float formWidth = pageSize.width;
float viewHeight = imageView.frame.size.height;
float formHeight = pageSize.height;
float widthRatio = viewWidth / formWidth;
float heightRatio = viewHeight / formHeight;
[self populateControls:NO
view:imageView
widthRatio:widthRatio
heightRatio:heightRatio];
[innerScroll addSubview:imageView];
[_theScrollView addSubview:innerScroll];
[self.views addObject:innerScroll];
i++;
}
[self.view addSubview:_theScrollView];
【问题讨论】:
-
您在哪个版本的 iOS 上运行它?另外,您是否检查过视图的边界(以及它的超级视图)?即使超级视图层次结构中的单个视图具有不正确的边界,它也可能与包含的视图发生冲突。
-
对不起,应该说它的 IOS7,我很确定在此之前它工作正常。我会按照建议进行检查。谢谢
-
这是每个视图的边界
-
尝试为每个视图添加不同颜色的边框。了解哪个视图实际在移动的最快方法。此外,
self.view.bounds可能并非总是返回屏幕尺寸(特别是在viewDidLoad中)。请改用[UIScreen mainScreen].bounds。设置边框后,您会看到框架是否正在调整大小,或者只是被翻译。 -
这帮助我理解了这个问题。它在下面的这篇文章中得到了回答。感觉有点尴尬,我自己没有发现,但我真的很感谢你让我走上正轨。谢谢! stackoverflow.com/questions/18824994/…
标签: ios objective-c cocoa-touch uiscrollview