我不想使用页面控制,因为我必须在用户水平滚动时更改按钮。
所以我正在使用UIScrollview和容器视图。
关注this tutorial 我能够在容器视图中添加子视图控制器,但滚动视图不会滚动自动布局。
这是我的代码
- (void)viewDidLoad {
[super viewDidLoad];
self.array_pageContent = [[NSMutableArray alloc] init];
CallViewController *objGameReviewPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CallViewController"];
[self addChildViewController:objGameReviewPageContentViewController];
UIView * view = objGameReviewPageContentViewController.view;
HomeViewController *objHomeViewController= [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
[self addChildViewController:objHomeViewController];
UIView * view1 = objHomeViewController.view;
GroupViewController *objGroupViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"GroupViewController"];
[self addChildViewController:objGroupViewController];
UIView * view2 = objGroupViewController.view;
CallenderViewController *objCallenderViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CallenderViewController"];
[self addChildViewController:objCallenderViewController];
UIView * view3 = objGroupViewController.view;
CasesViewController *objCasesViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CasesViewController"];
[self addChildViewController:objCasesViewController];
UIView * view4 = objGroupViewController.view;
[self.scrollViewContent setPagingEnabled:YES];
[self.scrollViewContent setScrollEnabled:YES];
[self.scrollViewContent setShowsHorizontalScrollIndicator:YES];
[self.scrollViewContent setShowsVerticalScrollIndicator:NO];
[self.scrollViewContent setDelegate:self];
[self.array_pageContent addObject:view];
[self.array_pageContent addObject:view1];
[self.array_pageContent addObject:view2];
[self.array_pageContent addObject:view3];
[self.array_pageContent addObject:view4];
NSInteger pageCount = self.array_pageContent.count;
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
self.mutableArray_pageContentViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.mutableArray_pageContentViews addObject:[NSNull null]];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGSize pagesScrollViewSize = self.scrollViewContent.frame.size;
self.scrollViewContent.contentSize = CGSizeMake(pagesScrollViewSize.width * self.array_pageContent.count, pagesScrollViewSize.height);
[self loadVisiblePages];
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.array_pageContent.count) {
return;
}
UIView *pageView = [self.mutableArray_pageContentViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
CGRect frame = self.scrollViewContent.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
UIView *newPageView = nil;
newPageView = [self.array_pageContent objectAtIndex:page];
newPageView.frame = frame;
[self.scrollViewContent addSubview:newPageView];
[self.mutableArray_pageContentViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.array_pageContent.count) {
return;
}
UIView *pageView = [self.mutableArray_pageContentViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.mutableArray_pageContentViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)loadVisiblePages {
CGFloat pageWidth = self.scrollViewContent.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollViewContent.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
self.pageControl.currentPage = page;
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
for (NSInteger i=lastPage+1; i<self.array_pageContent.count; i++) {
[self purgePage:i];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self loadVisiblePages];
}
答案 0 :(得分:2)
从视图层次结构中删除容器视图,您的滚动将起作用。
并从以下代码中重新命名您的viewDidLoad()元文件。
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat width = 0.0;
self.array_pageContent = [[NSMutableArray alloc] init];
CallViewController *objGameReviewPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CallViewController"];
[self addChildViewController:objGameReviewPageContentViewController];
width = objGameReviewPageContentViewController.view.frame.size.width;
[self.scrollViewContent addSubview:objGameReviewPageContentViewController.view];
// UIView * view = objGameReviewPageContentViewController.view;
HomeViewController *objHomeViewController= [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
CGRect frame = objHomeViewController.view.frame;
frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width;
objHomeViewController.view.frame = frame;
width += frame.size.width;
NSLog(@"%f",width);
[self addChildViewController:objHomeViewController];
[self.scrollViewContent addSubview:objHomeViewController.view];
// UIView * view1 = objHomeViewController.view;
GroupViewController *objGroupViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"GroupViewController"];
frame = objGroupViewController.view.frame;
frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width*2;
objGroupViewController.view.frame = frame;
width += frame.size.width;
NSLog(@"%f",width);
[self addChildViewController:objGroupViewController];
[self.scrollViewContent addSubview:objGroupViewController.view];
// [self addChildViewController:objGroupViewController];
// UIView * view2 = objGroupViewController.view;
CallenderViewController *objCallenderViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CallenderViewController"];
frame = objCallenderViewController.view.frame;
frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width*3;
objCallenderViewController.view.frame = frame;
width += frame.size.width;
NSLog(@"%f",width);
[self addChildViewController:objCallenderViewController];
[self.scrollViewContent addSubview:objCallenderViewController.view];
// [self addChildViewController:objCallenderViewController];
// UIView * view3 = objGroupViewController.view;
CasesViewController *objCasesViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CasesViewController"];
frame = objCasesViewController.view.frame;
frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width*4;
objCasesViewController.view.frame = frame;
width += frame.size.width;
NSLog(@"%f",width);
[self addChildViewController:objCasesViewController];
[self.scrollViewContent addSubview:objCasesViewController.view];
// [self addChildViewController:objCasesViewController];
// UIView * view4 = objGroupViewController.view;
[self.scrollViewContent setPagingEnabled:YES];
[self.scrollViewContent setScrollEnabled:YES];
[self.scrollViewContent setShowsHorizontalScrollIndicator:YES];
[self.scrollViewContent setShowsVerticalScrollIndicator:NO];
[self.scrollViewContent setDelegate:self];
self.scrollViewContent.contentSize = CGSizeMake(width, self.scrollViewContent.frame.size.height);
// [self.array_pageContent addObject:view];
// [self.array_pageContent addObject:view1];
// [self.array_pageContent addObject:view2];
// [self.array_pageContent addObject:view3];
// [self.array_pageContent addObject:view4];
// NSInteger pageCount = self.array_pageContent.count;
//
// self.pageControl.currentPage = 0;
// self.pageControl.numberOfPages = pageCount;
//
// self.mutableArray_pageContentViews = [[NSMutableArray alloc] init];
// for (NSInteger i = 0; i < pageCount; ++i) {
// [self.mutableArray_pageContentViews addObject:[NSNull null]];
// }
}
答案 1 :(得分:0)
请点击以下链接。这将解决您的问题。另外,请不要写太多代码,用故事板轻松完成。