• Snail—UI学习之自己定义标签栏UITabBarController


    这里的背景跟上面的差点儿相同




    只是这里要用到AppDelegate的单例进行传值

    首先到AppDelegate.h文件里

    <span style="color:#FF0000;">#import <UIKit/UIKit.h>
    
    @interface WJJRootViewController : UITabBarController
    
    //声明一UIButton属性 来记录当前按下的button
    @property (nonatomic,strong) UIButton * selectedButton;
    
    @end</span>

    然后到RootViewController.h中

    #import <UIKit/UIKit.h>
    
    @interface WJJRootViewController : UITabBarController
    
    //声明一UIButton属性 来记录当前按下的button
    @property (nonatomic,strong) UIButton * selectedButton;
    
    @end
    

    接下来进入到RootViewController.m中

    #import "WJJRootViewController.h"
    #import "WJJFirstViewController.h"
    #import "WJJSecondViewController.h"
    #import "WJJThirdViewController.h"
    #import "WJJForthViewController.h"
    <span style="color:#FF0000;">#import "WJJAppDelegate.h"</span>
    
    
    @interface WJJRootViewController ()
    
    @end
    
    @implementation WJJRootViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    //标签栏的高度是49像素
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view.
        [self createViewControllers];
        [self createMyTabBar];
    }
    
    - (void)createMyTabBar{
        
        //获取系统tabBar的位置
        CGRect frame = self.tabBar.frame;
        
        //创建一个UIView
        UIView * tabBarView = [[UIView alloc] initWithFrame:frame];
        tabBarView.backgroundColor = [UIColor blackColor];
        [self.view addSubview:tabBarView];
        
        <span style="color:#FF0000;">//把创建的tabBarView赋值给delegate的tabBarView属性
        WJJAppDelegate * delegate = [UIApplication sharedApplication].delegate;
        delegate.tabBarView = tabBarView;
        </span>
        
        //循环创建四个按钮 放到tabBarItem上
        CGFloat xSpace = (self.view.frame.size.width - 4 * 30) / 5;
        for (int i = 0; i < 4; i++) {
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(xSpace + i * (30 + xSpace), 9.5, 30, 30);
            //按钮寻常状态下得图片
            [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%d.png",i]] forState:UIControlStateNormal];
            //按钮点击时得图片颜色
            [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_c%d.png",i]] forState:UIControlStateSelected];
            [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 100 + i;
            [tabBarView addSubview:button];
            
            if (100 == button.tag) {
                //把第一个创建的按钮 设为默认
                button.selected = YES;
                //把点击的按钮 记录下来
                self.selectedButton = button;
            }
        }
    }
    
    - (void)click:(UIButton *)button{
        
        self.selectedButton.selected = YES;
        //运行以下代码 就能使得按不同的button调到不同界面
        self.selectedIndex = button.tag - 100;
        button.selected = YES;
        //把点击的按钮再次记录下来
        self.selectedButton = button;
        
    }
    
    - (void)createViewControllers{
        WJJFirstViewController * first = [[WJJFirstViewController alloc] init];
        
        UINavigationController * firstNav = [[UINavigationController alloc] initWithRootViewController:first];
        
        WJJSecondViewController * second = [[WJJSecondViewController alloc] init];
        
        WJJThirdViewController * third = [[WJJThirdViewController alloc] init];
        
        WJJForthViewController * forth = [[WJJForthViewController alloc] init];
        
        self.viewControllers = @[firstNav,second,third,forth];
    }
    
    - (void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
        //当此界面出现时 让系统的标签栏隐藏
        self.tabBar.hidden = YES;
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    然后进入到FirstViewController.m中 跟之前的是一样的代码

    #import "WJJFirstViewController.h"
    #import "WJJFirstChildViewController.h"
    
    
    @interface WJJFirstViewController ()
    
    @end
    
    @implementation WJJFirstViewController
    
    - (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];
        [self createButton];
    }
    
    - (void)createButton{
        
        UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
        rightButton.frame = CGRectMake(0, 0, 50, 20);
        [rightButton setTitle:@"下一页" forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem * rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        self.navigationItem.rightBarButtonItem = rightBarButtonItem;
        
    }
    
    - (void)nextPage{
        
        WJJFirstChildViewController * child = [[WJJFirstChildViewController alloc]init];
        [self.navigationController pushViewController:child animated:YES];
        
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    再次进入到FirstChildViewController.m 中

    #import "WJJFirstChildViewController.h"
    <span style="color:#FF0000;">#import "WJJAppDelegate.h"
    </span>
    @interface WJJFirstChildViewController ()
    
    @end
    
    @implementation WJJFirstChildViewController
    
    - (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 blueColor];
    }
    
    //用AppDelegate的单例模式传值
    
    
    //以下两个方法是进入到子界面时 tabBarView消失或隐藏
    - (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
       <span style="color:#FF0000;"> WJJAppDelegate * delegate = [UIApplication sharedApplication].delegate;
        delegate.tabBarView.hidden = YES;</span>
    }
    
    - (void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
        <span style="color:#FF0000;">WJJAppDelegate * delegate = [UIApplication sharedApplication].delegate;
        delegate.tabBarView.hidden = NO;</span>
    
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    在这里。我们是用得Appdelegate的单例模式进行对自己定义的tabBarView进行传值 设置它的隐藏 就是红色字体的代码

  • 相关阅读:
    学习素材、网站
    用 Python脚本生成 Android SALT 扰码
    H面试程序(29):求最大递增数
    常用数据库查询判断表和字段是否存在
    《火球——UML大战需求分析》(第3章 分析业务模型-类图)——3.7 关于对象图
    N个数依次入栈,出栈顺序有多少种
    WIN ERROR:C:WindowsSystem32<LANG_NAME>mstsc.exe.MUI
    大端法和小端法
    freopen()重定向的打开和关闭
    Linux 的 Spinlock 在 MIPS 多核处理器中的设计与实现
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7016779.html
Copyright © 2020-2023  润新知