UISegmentedControl的使用
最近项目中需要用到UISegmentedControl,在导航条上添加一个UISegmentedControl,点击不同的按钮,显示不同的ViewController,若是显示不同的tableview内容就很方便,直接刷新就可以了,但是现在时每个页面显示的内容不同,若是添加几个view来回隐藏也不方便。先看下效果图。
有两种实现方法,现在我们实现第一种。
我们先分别新建3个ViewController:申请OneViewController(页面的背景色为红色)、待办TwoViewController(页面的背景色为黄色)、测试ThreeViewController(页面的背景色为绿色)。
方法一:
#import "ViewController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#define COLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
@interface ViewController (){
NSMutableDictionary *listDic;
}
@property(nonatomic, strong) OneViewController *oneVC;
@property(nonatomic, strong) TwoViewController *twoVC;
@property(nonatomic, strong) ThreeViewController *threeVC;
@end
@implementation ViewController
-(OneViewController *)oneVC{
if (_oneVC == nil) {
_oneVC = [[OneViewController alloc] init];
}
return _oneVC;
}
-(TwoViewController *)twoVC{
if (_twoVC == nil) {
_twoVC = [[TwoViewController alloc] init];
}
return _twoVC;
}
-(ThreeViewController *)threeVC{
if (_threeVC == nil) {
_threeVC = [[ThreeViewController alloc] init];
}
return _threeVC;
}
- (void)viewDidLoad {
[super viewDidLoad];
listDic = [NSMutableDictionary dictionary];
NSArray * _titles = @[@"申请", @"待办",@"测试"];
UISegmentedControl * _segmentedControl = [[UISegmentedControl alloc] initWithItems:_titles];
_segmentedControl.selectedSegmentIndex = 0;
_segmentedControl.tintColor = COLOR(183, 222, 232, 1.0);
//修改字体的默认颜色与选中颜色
//选择后的字体颜色(在NSDictionary中 可以添加背景颜色和字体的背景颜色)
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],
NSForegroundColorAttributeName,
[UIFont systemFontOfSize:12],
NSFontAttributeName,nil];
[ _segmentedControl setTitleTextAttributes:dic forState:UIControlStateSelected];
//默认字体颜色
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:COLOR(183, 222, 232, 1.0),
NSForegroundColorAttributeName,
[UIFont systemFontOfSize:12],
NSFontAttributeName,nil];
[ _segmentedControl setTitleTextAttributes:dic1 forState:UIControlStateNormal];
[_segmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
//将segmentedControl添加到导航条上
self.navigationItem.titleView = _segmentedControl;
// 默认显示OneVc的内容
[self.view addSubview:self.oneVC.view];
}
//根据字典中是否存在相关页面对应的key,没有的话存储
- (UIViewController *)controllerForSegIndex:(NSUInteger)segIndex {
NSString *keyName = [NSString stringWithFormat:@"VC_%ld",segIndex];
UIViewController *controller = (UIViewController *)[listDic objectForKey:keyName];
if (!controller) {
if (segIndex == 0) {//申请
controller = self.oneVC;
}else if (segIndex == 1) {//待办
controller = self.twoVC;
}else{//测试
controller = self.threeVC;
}
[listDic setObject:controller forKey:keyName];
}
return controller;
}
//点击按钮事件
-(void)segmentValueChanged:(UISegmentedControl *)seg{
NSUInteger segIndex = [seg selectedSegmentIndex];
UIViewController *controller = [self controllerForSegIndex:segIndex];
//NSArray *array2 = [self.view subviews];
//NSLog(@"array2-->%@",array2);
//将当旧VC的view移除,然后在添加新VC的view
if (array2.count != 0) {
if (segIndex == 0) {
[_oneVC.view removeFromSuperview];
NSLog(@"remove--oneVC");
}else if (segIndex == 1){
[_twoVC.view removeFromSuperview];
NSLog(@"remove--twoVC");
}else{
[_threeVC.view removeFromSuperview];
NSLog(@"remove--threeVC");
}
}
[self.view addSubview:controller.view];
//NSArray *sub = [self.view subviews];
//NSLog(@"sub-->%@",sub);
}
方法二:利用addChildViewController来实现。
#import "NextViewController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#define COLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
@interface NextViewController (){
NSMutableDictionary *listDic;
}
@property(nonatomic, strong) OneViewController *oneVC;
@property(nonatomic, strong) TwoViewController *twoVC;
@property(nonatomic, strong) ThreeViewController *threeVC;
@property (nonatomic ,strong) UIViewController *currentVC;
@end
@implementation NextViewController
-(OneViewController *)oneVC{
if (_oneVC == nil) {
_oneVC = [[OneViewController alloc] init];
}
return _oneVC;
}
-(TwoViewController *)twoVC{
if (_twoVC == nil) {
_twoVC = [[TwoViewController alloc] init];
}
return _twoVC;
}
-(ThreeViewController *)threeVC{
if (_threeVC == nil) {
_threeVC = [[ThreeViewController alloc] init];
}
return _threeVC;
}
- (void)viewDidLoad {
[super viewDidLoad];
listDic = [NSMutableDictionary dictionary];
NSArray * _titles = @[@"申请", @"待办",@"测试"];
UISegmentedControl * _segmentedControl = [[UISegmentedControl alloc] initWithItems:_titles];
_segmentedControl.selectedSegmentIndex = 0;
_segmentedControl.tintColor = COLOR(183, 222, 232, 1.0);
//修改字体的默认颜色与选中颜色
//选择后的字体颜色(在NSDictionary中 可以添加背景颜色和字体的背景颜色)
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],
NSForegroundColorAttributeName,
[UIFont systemFontOfSize:12],
NSFontAttributeName,nil];
[ _segmentedControl setTitleTextAttributes:dic forState:UIControlStateSelected];
//默认字体颜色
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:COLOR(183, 222, 232, 1.0),
NSForegroundColorAttributeName,
[UIFont systemFontOfSize:12],
NSFontAttributeName,nil];
[ _segmentedControl setTitleTextAttributes:dic1 forState:UIControlStateNormal];
[_segmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
_segmentedControl.frame = CGRectMake(0.0, 0.0, 200.0, 29.0);
self.navigationItem.titleView = _segmentedControl;
//将默认的页面添加进去
[self addChildViewController:self.oneVC];
//设置当前显示的VC是哪个
self.currentVC = _oneVC;
}
//按钮点击事件
-(void)segmentValueChanged:(UISegmentedControl *)seg{
NSLog(@"seg.tag-->%ld",seg.selectedSegmentIndex);
switch (seg.selectedSegmentIndex) {
case 0:
[self replaceController:self.currentVC newController:self.oneVC];
break;
case 1:
[self replaceController:self.currentVC newController:self.twoVC];
break;
case 2:
[self replaceController:self.currentVC newController:self.threeVC];
break;
default:
break;
}
//NSArray *sub = self.childViewControllers;
//NSLog(@"sub-->%@",sub);
}
// 切换各个标签内容
- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
/**
* 着重介绍一下它
* transitionFromViewController:toViewController:duration:options:animations:completion:
* fromViewController 当前显示在父视图控制器中的子视图控制器
* toViewController 将要显示的姿势图控制器
* duration 动画时间
* options 动画效果
* animations 转换过程中得动画
* completion 转换完成
*/
[self addChildViewController:newController];
[self transitionFromViewController:oldController toViewController:newController duration:0.0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {
if (finished) {
[newController didMoveToParentViewController:self];
[oldController willMoveToParentViewController:nil];
[oldController removeFromParentViewController];
self.currentVC = newController;
}else{
self.currentVC = oldController;
}
}];
}
方法二:参考该链接