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

这里的背景跟上面的差不多

不过这里要用到AppDelegate的单例进行传值

首先到AppDelegate.h文件中

<span style="color:#FF0000;">#import <UIKit/UIKit.h>

@interface WJJRootViewController : UITabBarController

//声明一UIButton属性 来记录当前按下的按钮
@property (nonatomic,strong) UIButton * selectedButton;

@end</span>

然后到RootViewController.h中

#import <UIKit/UIKit.h>

@interface WJJRootViewController : UITabBarController

//声明一UIButton属性 来记录当前按下的按钮
@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进行传值 设置它的隐藏 就是红色字体的代码

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 12:35:23

Snail—UI学习之自定义标签栏UITabBarController的相关文章

Snail—UI学习之系统标签栏UITabBarController

背景条件是 有一个根控制器 继承于UITabBarController 然后 建四个UIViewController 再然后创建一个UIViewController 我们让它作为上面四个其中之一的子界面 然后再RootViewController中写入下面代码 #import "WJJRootViewController.h" #import "WJJFirstViewController.h" #import "WJJSecondViewControll

Snail—UI学习之自定义键盘及键盘收起(待完善)

在viewController.h中加入代理 #import <UIKit/UIKit.h> @interface WJJRootViewController : UIViewController <span style="color:#FF0000;"><UITextFieldDelegate></span> @end viewController.m中代码展示 #import "WJJRootViewController.h

Snail—UI学习之自定义导航栏NSNavigationController

首先新建一个RootViewController 再建一个FirstViewController 在RootViewController.m中写入 #import "WJJRootViewController.h" #import "WJJFirstViewController.h" @interface WJJRootViewController () @end @implementation WJJRootViewController - (id)initWit

Snail—UI学习之自定义通知NSNotification

背景是:一个界面跳转到第二个界面 然后 第一个界面发了一个通知  然后第二个界面收到这个通知后 把里面的数据取出来 在RootViewController.m中写入下面代码 #import "WJJRootViewController.h" #import "WJJFirstViewController.h" @interface WJJRootViewController (){ UITextField * _textField; } @end @implemen

Snail—UI学习之UITableView之自定义UITableViewCell

之前都是用得系统的UItableViewCell 局限性比较大 不够自由 自定义的cell满足了我们所有的需求 以后做项目的时候也是大部分都是用自定义的 后面我们要把数据放在数据模型里 数据源数组里将要存储的是model BookModel.h #import <Foundation/Foundation.h> @interface WJJBookModel : NSObject @property (nonatomic,copy) NSString * title; @property (n

Snail—UI学习之UITextField

简单看一下UITextField的属性 - (void)createTextField{ UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 40, 240, 40)]; //设置UITextField的边框风格,否则看不见textField 盲点的话可以点到它 /* UITextBorderStyleRoundedRect 圆角 UITextBorderStyleBezel 上.左有边框 UIT

Snail—UI学习之得到某组件的方法

第一种方法:根据传入函数的参数对象的tag属性区分 比如 多个按钮执行同一个方法,但是不同的方法执行时,里面的逻辑又不一样 那就得加以区分 这时可以用tag来区别 //再新建一个Button UIButton * button2 = [UIButton buttonWithType:UIButtonTypeSystem]; button2.frame = CGRectMake(20, 60, 280, 30); button2.tag = 2; button2.backgroundColor =

Snail—UI学习之初识

在AppDelegate.m中有几个默认存在的函数 // // WJJAppDelegate.m // 课上练习 // // Created by Snail on 15-7-20. // Copyright (c) 2015年 Snail. All rights reserved. // #import "WJJAppDelegate.h" @implementation WJJAppDelegate //程序的入口 仅仅同意一次 - (BOOL)application:(UIApp

Snail—UI学习之表视图TableView(一)

我们是整一个表视图 然后再表视图的上方是一个广告栏 首先,在.h文件中写上下面的代码 主要就是遵守一些代理 #import <UIKit/UIKit.h> @interface WJJRootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate> @end 然后再.m文件中写上如下 #import "WJJRootViewContro