我以前做过一个使用 UIScrollView 和 Paging 的项目,但是对于这个新项目,我一直在努力解决这个问题,但无法让它工作。我认为问题可能出在 AutoLayout 上。起初我无法让分页 Action 正常工作,直到我意识到所有初始化都必须在 viewDidAppear 而不是 ViewDidLoad 中完成。
现在我已经完成了分页操作, subview 不会加载!它只会将我在 Storyboard 中设置的当前 View 推开,只显示空白背景。
请注意,此处的代码与我之前的项目几乎相同,但我无法显示第二个 View (第 2 页)。
部分代码如下:
- (void)viewDidAppear:(BOOL)animated{
self.scrollView.delegate = self;
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++)
{
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, (scrollView.frame.size.height - 50));
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
//
[self loadScrollViewWithPage:1];
}
- (void)loadScrollViewWithPage:(int)page
{
if (page <= 0)
return;
if (page >= kNumberOfPages)
return;
PageOneViewController *controller = [viewControllers objectAtIndex:1];
if ((NSNull *)controller == [NSNull null])
{
if(page == 1)
{
controller = [[PageOneViewController alloc] initWithRequestNumber: @"100" forID:self.idNumberFromLogin];
[viewControllers replaceObjectAtIndex:page withObject:controller];
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * 1;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
}
在 PageOneViewController 中
- (id)initWithRequestNumber:(NSString *)requestID forID: (NSString *) employeeID
{
if (self = [super initWithNibName:@"OpenRequestCommView" bundle:nil])
{
}
return self;
}
最佳答案
您可能在 Storyboard上启用了自动布局。
您可以通过在 Storyboard中选择文件检查器并取消选中“使用自动布局”来禁用自动布局。
关于objective-c - XCode - UIScrollView 分页不显示 subview (自动布局?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13406335/