框架搭建_纯代码

目录:
1、利用ViewController中间过渡
2、直接设置UITabBarController的数组

比较:相对来说第2中方式较方便,设置内容、标题等比较清晰明确,第1中方式中设置标题等内容时容易搞混,相对第2中方式没有较大的优势。

1、利用ViewController中间过渡

AppDelegate中代码设置

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
    
    return YES;
}

ViewController中代码设置

    //初始化要使用的三个VC
    FirstVc *first = [[FirstVc alloc] init];
    SecondVc *second = [[SecondVc alloc] init];
    ThirdVc *third = [[ThirdVc alloc] init];
    
    //初始化三个nav
    UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:first];
    firstNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"111" image:nil tag:1];
    
    UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:second];
    secondNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:nil tag:1];

    UINavigationController *thirdNav = [[UINavigationController alloc] initWithRootViewController:third];
    thirdNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"333" image:nil tag:1];

    //初始化UITabBarController
    self.tabBarCont = [[UITabBarController alloc] init];
    self.tabBarCont.viewControllers = @[firstNav,secondNav,thirdNav];
    
    self.tabBarCont.selectedIndex = 0;
    self.tabBarCont.delegate = self;
    self.tabBarCont.view.frame = self.view.frame;
    
    [self.view addSubview:self.tabBarCont.view];

各个VC中的设置

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.title = @"first";//此设置改变nav的标题和底部item的标题
    self.navigationController.title = @"111";//item标题
}

2、直接设置UITabBarController的数组

AppDelegate中设置

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //初始化
    FirstVc *first = [[FirstVc alloc] init];
    SecondVc *second = [[SecondVc alloc] init];
    ThirdVc *third = [[ThirdVc alloc] init];
    
    //创建标签栏控制器
    tabBarControl = [UITabBarController new];
    
    //放入标签栏中
    tabBarControl.viewControllers = @[first,second,third];
    
    //创建导航栏控制器,并指定他的根视图控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tabBarControl];
    
    //添加中间的按钮
    //缺点:1、按钮超过bottom的部分点击无响应    2、点击除按钮外的中间部分时也展示中间item的页面
    //3个item是可能范围太大,如果调整item数量,缺点2应该影响不大。
    UIButton *btn = [[UIButton alloc] init];
    btn.backgroundColor = [UIColor redColor];
    [btn setFrame:CGRectMake(130, -12, 60, 60)];
    btn.clipsToBounds = YES;
    btn.layer.cornerRadius = 30;
    [btn addTarget:self action:@selector(clickCenterButton) forControlEvents:UIControlEventTouchUpInside];
    
    [tabBarControl.tabBar addSubview:btn];
    
    //指定应用的跟视图控制器
    self.window.rootViewController = nav;
    
    return YES;
}

各个VC中的设置

- (instancetype)init
{
    self = [super init];
    
    if (self)
    {
        self.title = @"111";//此处的title是item的标题
        
        //设置图标的默认图片和选中后图片
        self.tabBarItem.image = [[UIImage imageNamed:@"tabbar_mainframe@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_mainframeHL@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        //未读消息数量(右上角标识)
        [self.tabBarItem setBadgeValue:@"18"];
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.navigationItem.title = @"first";//共同使用一个navigation,要在此方法中设置名称
    
    //设置右边导航按钮
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(tapAdd)];

    self.tabBarController.navigationItem.rightBarButtonItem = rightItem;
}

- (void)tapAdd
{
    //此处跳转页面操作
    //或者弹出页面
}

你可能感兴趣的:(框架搭建_纯代码)