1

我想知道是否有简单或简洁的方法可以UIScrollView根据页码实现不同大小和框架的来源?我想在第一页显示导航栏,但在其他页面上隐藏它。
我想到的第一个想法是使用scrollViewDidScroll:(UIScrollView *)scrollView方法,我可以通过setFrame:(CGRect).
这是一团糟,因为我在scrollView其他地方定义了这个,初始ContentSize页数和页数等等。所以实际上,在加载整个视图之后,当用户滚动视图时,我必须处理框架变化。 我现在拥有的:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageController.currentPage = page;
    [self.navigationController.navigationBar setHidden:page];
    if (page) {
        [self.view setFrame:CGRectMake(0, -44, self.view.frame.size.width, self.view.frame.size.height + 44)];
        [self.view setBackgroundColor:[UIColor redColor]];
        [self.scrollView setFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        [self.scrollView setBackgroundColor:[UIColor greenColor]];
    }else{
        [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        [self.view setBackgroundColor:[UIColor redColor]];
        [self.scrollView setFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        [self.scrollView setBackgroundColor:[UIColor greenColor]];
    }
}

颜色仅用于视觉调试。把它想象成跳板,不时出现导航栏。scrollView部分视图也是如此,而不是整个视图。不确定这个想法是否值得,但我不是那个发号施令的人,所以我正在努力寻找任何东西。是这样吗?

4

1 回答 1

0

我终于想出了这个(里面放了相同的代码-(void)scrollViewDidScroll:

-(void)viewWillAppear:(BOOL)animated{
NSLog(@"HomeVC: viewWillAppear");
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageController.currentPage = page;
if (page > 0) {
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 44)];
    [self.view setBackgroundColor:[UIColor clearColor]];
    [self.scrollView setFrame:CGRectMake(0, 0, HOMESCREEN_WIDTH, HOMESCREEN_HEIGHT + 44)];
    [self.scrollView setContentSize:CGSizeMake(HOMESCREEN_WIDTH * SCREEN, HOMESCREEN_HEIGHT + 44)];
    [self.scrollView setBackgroundColor:[UIColor clearColor]];
    [self.pageController setCenter:CGPointMake(HOMESCREEN_WIDTH/2, self.scrollView.frame.size.height + 5)];
}else{
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view setBackgroundColor:[UIColor redColor]];
    [self.scrollView setFrame:CGRectMake(0, 0, HOMESCREEN_WIDTH, HOMESCREEN_HEIGHT)];
    [self.scrollView setContentSize:CGSizeMake(HOMESCREEN_WIDTH * SCREEN, HOMESCREEN_HEIGHT)];
    [self.scrollView setBackgroundColor:[UIColor clearColor]];
    [self.pageController setCenter:CGPointMake(HOMESCREEN_WIDTH/2, self.scrollView.frame.size.height + 5)];
}
[self.navigationController setToolbarHidden:YES animated:YES];
}

这是工作!

于 2013-06-11T11:00:34.287 回答