• 分栏控制器


     

     

     

    分栏控制器UITabBarController


    **********************************************************

    //首先将导航控制器或视图控制器放到数组中controllerArr

    //创建一个分栏控制器的实例对象
    UITabBarController *tbc = [[UITabBarController alloc]init];
    //将页面数组设置给分栏控制器
    tbc.viewControllers = controllerArr;
    //内容渲染色,和navigationbar的设置类似
    tbc.tabBar.tintColor = [UIColor redColor];
    //背景色
    tbc.tabBar.barTintColor = [UIColor grayColor];
    //背景图片
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabbg"];
    //将分栏控制器设为window的根控制器
    self.window.rootViewController = tbc;


    FiveViewController *fvc = [[FiveViewController alloc]init];
    //弹出新页面时隐藏tabBar
    fvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:fvc animated:YES];

     

    **********************************************************

    UITabBarItem分栏控制器专用按钮

    //使用系统样式,自带图片和title
    - (id)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem tag:(NSInteger)tag;

    //使用1张图片和文字
    - (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag;

    //使用2张图片和文字
    - (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage;

    //item中的图片都是取形状然后进行渲染,如果不想渲染,而是想用图片本身的颜色,让图片调个方法

    //返回值也是图片,并且这个图片不会被渲染,而是使用图片本身的颜色
    //[[UIImage imageNamed:@"tab_0"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]


    **********************************************************

    自定义tabBar思路:

    一、设置tbc(tabBarController)的viewControllers,

    二、把一个包含button和label的视图放到tabBar上,

    三、button的点击事件里处理变色以及设置tbc的selectedIndex

     

     

     

     


    #import "MyTabBar.h"
    #import "LoveViewController.h"
    #import "NearViewController.h"
    #import "ShareViewController.h"
    #import "SetViewController.h"

    @interface MyTabBar ()

    @end

    @implementation MyTabBar

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    /*
    * 创建tabBar基本流程
    *
    * 1,创建想要展示的页面
    * 2,如果需要,将页面放到导航里
    * 3,设置导航(页面)对应的显示在tabBar上的专用按钮
    * 4,将导航(页面)放到数组里设置给tabBar
    *
    */

    LoveViewController *loveVC = [[LoveViewController alloc] init];
    NearViewController *nearVC = [[NearViewController alloc] init];
    ShareViewController *shareVC = [[ShareViewController alloc] init];
    SetViewController *setVC = [[SetViewController alloc] init];

    UINavigationController *loveNC = [[UINavigationController alloc] initWithRootViewController:loveVC];
    UINavigationController *nearNC = [[UINavigationController alloc] initWithRootViewController:nearVC];

    loveVC.navigationItem.title = @"最爱";
    nearVC.navigationItem.title = @"附近";

    //创建一个分栏上展示的专用按钮(用系统样式)
    UITabBarItem *loveItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];
    //将专用按钮设置给对应的页面或者导航
    loveNC.tabBarItem = loveItem;

    //使用2张图片创建专用按钮(默认情况下只取形状,颜色会被渲染掉)
    //给tabBarItem设置的图片最合适的大小是30*30
    UITabBarItem *nearItem = [[UITabBarItem alloc] initWithTitle:@"near" image:[UIImage imageNamed:@"tab0_1"] selectedImage:[UIImage imageNamed:@"tab0_2"]];
    nearNC.tabBarItem = nearItem;

    //使用1张图片创建专用按钮
    UITabBarItem *shareItem = [[UITabBarItem alloc] initWithTitle:@"share" image:[UIImage imageNamed:@"tab_0"] tag:0];
    shareVC.tabBarItem = shareItem;

    //使用2张图片创建,(使用原来的图片进行展示,不会被渲染)
    UITabBarItem *setItem = [[UITabBarItem alloc] initWithTitle:@"set" image:[[UIImage imageNamed:@"tab_2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tab_c2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    setVC.tabBarItem = setItem;

    //设置专用按钮的字体
    [setItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil] forState:UIControlStateNormal];

    //设置tabBar需要展示的页面或导航
    self.viewControllers = [NSArray arrayWithObjects:loveNC, nearNC,shareVC, setVC, nil];

    //通过分栏控制器找到标签栏,设置渲染色
    self.tabBar.tintColor = [UIColor redColor];

    //设置背景色
    self.tabBar.barTintColor = [UIColor blackColor];

    //背景图片(注意标签栏的高度是49)
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabbg"];
    }

     

     

     


    #import "LoveViewController.h"
    #import "DetailViewController.h"

    @interface LoveViewController ()

    @end

    @implementation LoveViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    }

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    DetailViewController *dvc = [[DetailViewController alloc] init];

    //当二级页面被弹出的时候,隐藏底部的bar
    dvc.hidesBottomBarWhenPushed = YES;

    dvc.view.backgroundColor = [UIColor blueColor];

    [self.navigationController pushViewController:dvc animated:YES];
    }

     

     

     


    #import "DetailViewController.h"

    @interface DetailViewController ()

    @end

    @implementation DetailViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    }

     

     

     

     

    #import "NearViewController.h"

    @interface NearViewController ()

    @end

    @implementation NearViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    }

     

     

     


    #import "ShareViewController.h"

    @interface ShareViewController ()

    @end

    @implementation ShareViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    }

     

     


    #import "SetViewController.h"

    @interface SetViewController ()

    @end

    @implementation SetViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    }

     


    -----------------------------------------------------------------分割----------------------------------------------------------------

     

     

     

     

    #import "MyTabBar.h"
    #import "HomeViewController.h"
    #import "LoveViewController.h"
    #import "NearViewController.h"
    #import "SetViewController.h"

    @interface MyTabBar ()

    @end

    @implementation MyTabBar

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self customVC];

    [self customTabBar];
    }

    - (void)customVC
    {//定义viewController
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    LoveViewController *loveVC = [[LoveViewController alloc] init];
    NearViewController *nearVC = [[NearViewController alloc] init];
    SetViewController *setVC = [[SetViewController alloc] init];

    UINavigationController *homeNC = [[UINavigationController alloc] initWithRootViewController:homeVC];
    homeVC.navigationItem.title = @"主页";

    self.viewControllers = [NSArray arrayWithObjects:homeNC, loveVC, nearVC, setVC, nil];
    }

    - (void)customTabBar
    {//定义tabBar
    //背景图片
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 49)];
    bgView.image = [UIImage imageNamed:@"tab_bg"];
    bgView.userInteractionEnabled = YES;
    [self.tabBar addSubview:bgView];

    NSArray *titleArr = [NSArray arrayWithObjects:@"首页", @"最爱", @"附近", @"设置", nil];
    for (int i = 0; i<4; i++) {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(25+i*80, 3, 30, 30);
    [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%d",i]] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_c%d",i]] forState:UIControlStateSelected];
    btn.tag = 1000+i;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:btn];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(-5, 33, 40, 12)];
    label.font = [UIFont systemFontOfSize:10];
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [titleArr objectAtIndex:i];
    [btn addSubview:label];

    if (!i) {
    btn.selected = YES;
    label.textColor = [UIColor orangeColor];
    }
    }
    }

    - (void)btnClick:(UIButton *)sender
    {
    for (int i = 0; i<4; i++) {
    UIButton *btn = (UIButton *)[self.tabBar viewWithTag:1000+i];
    btn.selected = NO;
    UILabel *label = [[btn subviews] lastObject];
    label.textColor = [UIColor grayColor];
    }

    sender.selected = YES;
    UILabel *label = [[sender subviews] lastObject];
    label.textColor = [UIColor orangeColor];

    //展示tabBarController里的页面(通过下标)
    self.selectedIndex = sender.tag - 1000;
    }

     

     

     

     

    #import "HomeViewController.h"
    #import "SetViewController.h"

    @interface HomeViewController ()

    @end

    @implementation HomeViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    }

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    SetViewController *svc = [[SetViewController alloc] init];
    svc.hidesBottomBarWhenPushed = YES;
    svc.view.backgroundColor = [UIColor cyanColor];
    [self.navigationController pushViewController:svc animated:YES];
    }

     

     

     

     

    #import "LoveViewController.h"

    @interface LoveViewController ()

    @end

    @implementation LoveViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    }

     

     

     

     


    #import "NearViewController.h"

    @interface NearViewController ()

    @end

    @implementation NearViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    }

     

     

     

     

     


    #import "SetViewController.h"

    @interface SetViewController ()

    @end

    @implementation SetViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    }

     

    让明天,不后悔今天的所作所为
  • 相关阅读:
    6-1
    5-9
    5-8
    5-7
    5-6
    实验4-1:掌握Android应用调试方法、添加新界面
    实验3:理解Activity 的生命周期
    R.java常见问题解决方案
    配置Android模拟器
    第02周实验: 变量、算术运算和数据类型
  • 原文地址:https://www.cnblogs.com/-yun/p/4379061.html
Copyright © 2020-2023  润新知