iOS开发中的小技巧4:简单的头部标题+底部滑动

在开发中经常会用头部标题+底部轮播或者页面转换,类似于头条效果,此时不能在UIScrollView+UITableView混合使用,如果使用UItableView的话,加载数据时滑动会导致程序崩溃,所以需要使用addChildViewController方法;


iOS开发中的小技巧4:简单的头部标题+底部滑动_第1张图片

这里写一个简单的实例,标题栏仅写两个

总体布局为头部UIView上添加button,底部添加scrollView,scrollView上添加子控制器。

准备工作,新建两个ViewController:TestOneVC、TestTwoVC

1)新建UIView,命名为kndView;新建两个button:one、two;新建一个scrollView;

@property(nonatomic,strong)UIView *kindView;

@property(nonatomic,strong)UIButton *one;

@property(nonatomic,strong)UIButton *two;

@property(nonatomic,strong)UIScrollView *scrollView;

2)初始化kndView、one、two、scrollView

self.kindView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_W, 44)];

self.kindView.backgroundColor = [UIColor colorWithHexString:@"FFFFFF"];

[self.view addSubview:self.kindView];

self.one = [UIButton buttonWithType:(UIButtonTypeCustom)];

[self.one setTitle:@"一" forState:(UIControlStateNormal)];

[self.one addTarget:self action:@selector(oneAction) forControlEvents:(UIControlEventTouchUpInside)];

self.one.frame = CGRectMake(0, 0, SCREEN_W/2, 44);

[self.kindView addSubview:self.one];

[self.one setTitleColor:[UIColor colorWithHexString:@"030303"] forState:(UIControlStateNormal)];

self.one.titleLabel.font = [UIFont systemFontOfSize:18];

self.two = [UIButton buttonWithType:(UIButtonTypeCustom)];

[self.two setTitle:@"er" forState:(UIControlStateNormal)];

[self.two addTarget:self action:@selector(twoAction) forControlEvents:(UIControlEventTouchUpInside)];

self.two.frame = CGRectMake(SCREEN_W/2, 0, SCREEN_W/2, 44);

[self.kindView addSubview:self.two];

[self.two setTitleColor:[UIColor colorWithHexString:@"999999"] forState:(UIControlStateNormal)];

self.two.titleLabel.font = [UIFont systemFontOfSize:16];

self.scrollView = [[UIScrollView alloc] init];

[self.view addSubview:self.scrollView];

self.scrollView.frame = CGRectMake(0, CGRectGetMaxY(self.kind.frame), SCREEN_W, SCREEN_H-64-44-44);

self.scrollView.delegate = self;

self.scrollView.contentSize = CGSizeMake(SCREEN_W*2, 0);

self.scrollView.bounces = NO;

self.scrollView.pagingEnabled = YES;

self.scrollView.showsHorizontalScrollIndicator = NO;

self.scrollView.scrollEnabled = NO;

self.scrollView.delegate = self;

3)添加第一个子控制器

TestOneVC *one =  [[TestOneVC alloc] init];

[self.scrollView addSubview:one.view];

[self addChildViewController:one];

[self.view insertSubview:self.scrollView belowSubview:self.kindView];

4)创建剩余的子控制器

- (void)setupChildViewController{

TestTwoVC *two = [[TestTwoVC alloc] init];

[self addChildViewController:two];

}

5)button方法+子控制器选择方法

- (void)showVc:(NSInteger)index {

CGFloat offsetX = index * SCREEN_W;

UIViewController *vc = self.childViewControllers[index];

// 判断控制器的view有没有加载过,如果已经加载过,就不需要加载

if (vc.isViewLoaded) return;

[self.backScroll addSubview:vc.view];

vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);

}

-(void)oneAction{

self.scrollView.contentOffset = CGPointMake(0, 0);

[self.one setTitleColor:[UIColor colorWithHexString:@"030303"] forState:(UIControlStateNormal)];

self.one.titleLabel.font = [UIFont systemFontOfSize:18];

[self.two setTitleColor:[UIColor colorWithHexString:@"999999"] forState:(UIControlStateNormal)];

self.two.titleLabel.font = [UIFont systemFontOfSize:16];

[self showVc:0];

}

-(void)twoAction{

[self showVc:1];

self.backScroll.contentOffset = CGPointMake(SCREEN_W, 0);

[self.one setTitleColor:[UIColor colorWithHexString:@"999999"] forState:(UIControlStateNormal)];

self.one.titleLabel.font = [UIFont systemFontOfSize:16];

[self.two setTitleColor:[UIColor colorWithHexString:@"030303"] forState:(UIControlStateNormal)];

self.two.titleLabel.font = [UIFont systemFontOfSize:18];

}

6)如果想要滑动,在scrollView的代理方法中添加一下方法:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

if (scrollView.contentOffset.x/SCREEN_W==0) {

[self.one setTitleColor:[UIColor colorWithHexString:@"030303"] forState:(UIControlStateNormal)];

elf.one.titleLabel.font = [UIFont systemFontOfSize:18];

self.two.titleLabel.font = [UIFont systemFontOfSize:16];

[self showVc:0];

}else{

[self.one setTitleColor:[UIColor colorWithHexString:@"999999"] forState:(UIControlStateNormal)];

self.one.titleLabel.font = [UIFont systemFontOfSize:16];

[self.two setTitleColor:[UIColor colorWithHexString:@"030303"] forState:(UIControlStateNormal)];

self.two.titleLabel.font = [UIFont systemFontOfSize:18];

[self showVc:1];

}

}

7)细节处理

self.automaticallyAdjustsScrollViewInsets = NO;//不让view乱跑

TestOneVC *one = [[TestOneVC alloc] init];

[self.scrollView addSubview:one.view];//添加one的view控制器到scrollView上,已经添加过了,所以创建子控制器时不需要再添加这一个了

[self addChildViewController:one];//添加子控制器

[self.view insertSubview:self.scrollView belowSubview:self.kindView];将self.scrollView添加在kindView的下边

你可能感兴趣的:(iOS开发中的小技巧4:简单的头部标题+底部滑动)