【发布时间】:2012-11-15 21:15:13
【问题描述】:
我之前制作了一个使用 UIScrollView 和 Paging 的项目,但是在这个新项目中,我一直在努力解决这个问题,无法让它工作。我认为问题可能在于 AutoLayout。起初,我什至无法让分页动作起作用,直到我意识到所有初始化都必须在 viewDidAppear 而不是 ViewDidLoad 中完成。
现在我已经完成了分页操作,子视图不加载!它只会将我在 Storyboard 中设置的当前视图推开,只显示空白背景。
请注意,这里的代码与我之前的项目几乎相同,但我无法显示第二个视图(第 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;
}
【问题讨论】:
-
你能用自动布局来解决这个问题吗?
标签: objective-c ios xcode uiscrollview autolayout