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

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


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

总体布局为头部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的下边

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一:在ViewController中实例化MLPickerScrollView完成代理<MLPickerScrol...
    欧大帅Allen阅读 6,995评论 1 9
  • 1.NSString过滤特殊字符串定义一个特殊字符的集合NSCharacterSet set = [NSChara...
    奋拓达阅读 778评论 0 0
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,562评论 1 14
  • 打印View所有子视图 layoutSubviews调用的调用时机 当视图第一次显示的时候会被调用当这个视图显示到...
    hyeeyh阅读 534评论 0 3
  • 如果说人生就是一场修行,无论你经历过什么,在经历什么,那都是前生的因今生的果,那么渊源轮回,宿命辗转无论对与错,爱...
    慕汐繁星阅读 279评论 0 0