{
// 注意: 顺序(类似压栈的方式)
// 创建VC对象
self.gameVC = [[GameListViewController alloc] init];
[self addChildViewController:self.gameVC];
[self.view addSubview:self.gameVC.view];
self.TVVC = [[TVListViewController alloc] init];
[self addChildViewController:self.TVVC];
[self.view addSubview:self.TVVC.view];
self.movieVC = [[MovieListViewController alloc] init];
[self addChildViewController:self.movieVC];
[self.view addSubview:self.movieVC.view];
self.view.backgroundColor = [UIColor whiteColor];
[self setAutomaticallyAdjustsScrollViewInsets:NO];
// 创建分段控制器对象
UISegmentedControl *segmentC = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"综艺", @"电视", @"游戏", nil]];
segmentC.backgroundColor = [UIColor groupTableViewBackgroundColor];
// 定位置
segmentC.frame = CGRectMake(100 * k_WIDTH, 30 * k_HEIGHT, 210 * k_WIDTH, 30 * k_HEIGHT);
// 设置圆角
segmentC.layer.masksToBounds = YES;
segmentC.layer.cornerRadius = 5;
// 设置颜色
segmentC.layer.borderColor = [UIColor colorWithRed:1.000 green:0.502 blue:0.000 alpha:1.000].CGColor;
// 设置边框粗细
segmentC.layer.borderWidth = 1;
// 设置默认0
segmentC.selectedSegmentIndex = 0;
self.navigationItem.titleView = segmentC;
[segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
}
// segment点击事件实现方法
- (void)segmentAction:(UISegmentedControl *)segment {
// 如果是0 就进入综艺界面
if (segment.selectedSegmentIndex == 0) {
[self.view bringSubviewToFront:self.movieVC.view];
}
if (segment.selectedSegmentIndex == 1) {
[self.view bringSubviewToFront:self.TVVC.view];
}
if (segment.selectedSegmentIndex == 2) {
[self.view bringSubviewToFront:self.gameVC.view];
}
}
UISegmentedControl分段控制器
最新推荐文章于 2018-05-15 11:09:55 发布