UI控件笔记(六):UI之UINavigationController、navigationBar和UIView的封装

一、UINavigationController导航控制器

1AppDelegate.m文件中的写法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];


    1.1、实例化一个VC

    FirstViewController *first = [[FirstViewController alloc] init];


  1.2、实例化一个导航控制器,把上面的VC当做这个导航控制器的根VC,也就是栈里面最下面一个对象

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:first];


    注意:当一个VC在导航控制器的栈中的时候,才能用到导航控制器的功能

    

    [first release];

    

    1.3、因为导航控制器是VC的子类,所以也是一个VC

    self.window.rootViewController = nav;

    

    [nav release];

    [self.window makeKeyAndVisible];

    return YES;

}


2FirstViewController.m文件中的写法

#import "FirstViewController.h"

#import "SecondViewController.h"


//ios6之前状态条、导航条是分离的,ios7之后,状态条的颜色会被导航条影响

//导航条高度:44


@interface FirstViewController ()


@end


@implementation FirstViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor redColor];

    

    [self makeBtn];

    // Do any additional setup after loading the view.

}


-(void)makeBtn

{

    //ios6之前,导航条下面是0ios7之后,导航条下面是64

    //有导航的话,ios6y起点还是0,就是从导航条下沿开始的,ios7y的起点应该是64,也是从导航条下沿开始

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(200, 64, 80, 40);

    [btn setTitle:@"toSecond" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(btnDown) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}


-(void)btnDown

{

    /*

        在这里从第一个页面push到了第二个页面

        1、注意:首先要保证当前VC在一个导航控制器里

        2、从ApushB就是一个入栈的操作

        3、在这里把second这个VC入栈到了导航控制器的栈中,second引用计数+1

        4、栈的概念:先进后出、后进先出

        5、所以现在导航控制器的栈里,按照先后顺序有了两个,first先在下,second后在上

        6、未来也肯定是后面的second先出栈,second出栈了,才能看见下面的first

    */

    SecondViewController *second = [[SecondViewController alloc] init];

    //要去第二个页面,先要实例化第二个页面的对象

    [self.navigationController pushViewController:second animated:YES];

    [second release];

}


3、页面回跳类型

-(void)backToThird

{

    //直接回到现在之前的某个页面

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];

    //第一个参数是当前VC所在的导航控制器中的所有VC数组中的某一个下标所代表的那个VC

    //self.navigationController.viewControllers就是存了当前导航控制器里所有VC的数组

    //通过下标找到了第三个VC

}


-(void)backToRoot

{

    //直接回到导航的第一个VC,也就是根视图控制器

    [self.navigationController popToRootViewControllerAnimated:YES];

    //就是把根视图控制器之后的所有VC出栈

}


二、navigationBar导航条


   1、设置导航条的颜色

    [self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];


    2、设置导航条样式

    self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;

    //self.navigationController.navigationBar 找到当前VCself)的导航控制器(navigationController)的导航条(navigationBar),首先要保证当前VCself)在一个导航里

    

    3、设置导航条的背景图片

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBg-32.png"] forBarMetrics:UIBarMetricsCompact];//横屏时

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBg.png"] forBarMetrics:UIBarMetricsDefault];//竖屏时

    

    self.title = @"xixixi";

注意:第二个页面的导航条和第一个页面的一样,整个导航控制器公用一个导航条,所以一经设置,永久拥有


4、导航条上面的item


- (void)viewDidLoad {

    [super viewDidLoad];

    

    4.1title

    self.navigationItem.title = @"第二个页面";//设置导航条的title

    self.title = @"pp";

    

    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];

    titleView.backgroundColor = [UIColor yellowColor];

    self.navigationItem.titleView = titleView;

    [titleView release];

    //可以设置导航条上title的图片,正常应该是一个UIImageView

    

    4.2barButtonItem(看上去是btn,其实不是btn,但是用起来和btn一样)


4.2.1、用文本的方式实例化

    UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"goBack" style:UIBarButtonItemStyleDone target:self action:@selector(leftDown)];//用文本的方式实例化


4.2.2、用图片的方式实例化

    UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"itemImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftDown)];//用图片的方式实例化


     4.2.2、用一个UIView的子类来实例化

    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    leftBtn.frame = CGRectMake(0, 0, 60, 30);

    [leftBtn setTitle:@"hahaha" forState:UIControlStateNormal];

    [leftBtn addTarget:self action:@selector(leftDown) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];//用一个UIView的子类来实例化

    

    self.navigationItem.leftBarButtonItem = leftBarBtnItem;

    [leftBarBtnItem release];


    4.3barButtonItems

    UIBarButtonItem *rightFirstItem = [[UIBarButtonItem alloc] initWithTitle:@"pp" style:UIBarButtonItemStylePlain target:self action:@selector(leftDown)];//右侧第一个item

    UIBarButtonItem *rightSecondItem = [[UIBarButtonItem alloc] initWithTitle:@"qq" style:UIBarButtonItemStylePlain target:self action:@selector(leftDown)];//右侧第二个item

    

    //把两个item都放进一个数组

    NSArray *itemArr = @[rightFirstItem,rightSecondItem];

    [rightFirstItem release];

    [rightSecondItem release];

    

    self.navigationItem.rightBarButtonItems = itemArr;//用存着item的数组来给导航条的item赋值

    //按照数组中的顺序 自右向左

    

    // Do any additional setup after loading the view.

}


-(void)leftDown//导航条左item的事件

{

    [self.navigationController popViewControllerAnimated:YES];

}


三、自定义导航条和UIView的封装

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //self.navigationController.navigationBar setBackgroundImage:<#(UIImage *)#> forBarMetrics:<#(UIBarMetrics)#>系统导航条的设置

    

    1、隐藏系统导航条

    self.navigationController.navigationBarHidden = YES;

    

    2、实现自定义的条(View

    [self makeMyNav];

    // Do any additional setup after loading the view.

}


-(void)makeMyNav

{

  3UIView封装调用

    NavView *nav = [[NavView alloc] initWithFrame:CGRectMake(0, 20, 320, 44) andTitle:@"主页" andLeftBtnImage:nil andleftBtnSelector:@selector(pp) andRightBtnImage:@"ico_p2.png" andRightBtnSelector:@selector(rightDown) andTarget:self];

    [self.view addSubview:nav];

    [nav release];

}


-(void)leftDown

{

    NSLog(@"啥也不干");

}


-(void)rightDown

{

    NSLog(@"2");

    SecondViewController *second = [[SecondViewController alloc] init];

    [self.navigationController pushViewController:second animated:YES];

    [second release];

}


注意:自定义View里面的UIframe应该相对于自定义的View,而不是整个页面


4UIView封装:NavView.h

#import <UIKit/UIKit.h>


@interface NavView : UIView


-(id)initWithFrame:(CGRect)rect andTitle:(NSString*)title andLeftBtnImage:(NSString*)leftImage andleftBtnSelector:(SEL)leftSelector andRightBtnImage:(NSString*)rightImage andRightBtnSelector:(SEL)rightSelector andTarget:(id)target;


@end


5UIView封装:NavView.m

#import "NavView.h"


@implementation NavView

//因为这个类是继承于UIView的子类,所以这个类也是一个View,所以在这个类力,self就表示这个类所代表的View

-(id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if(self)

    {

        

    }

    return self;

}//UIView的子类的主方法就是init方法


-(id)initWithFrame:(CGRect)rect andTitle:(NSString *)title andLeftBtnImage:(NSString *)leftImage andleftBtnSelector:(SEL)leftSelector andRightBtnImage:(NSString *)rightImage andRightBtnSelector:(SEL)rightSelector andTarget:(id)target

{

    self = [super initWithFrame:rect];

    if(self)

    {

        //在这里调用下面的布局方法,把init方法中的参数当做下面布局方法的参数

        

        [self makeUIWithTitle:title andLeftBtnImage:leftImage andleftBtnSelector:leftSelector andRightBtnImage:rightImage andRightBtnSelector:rightSelector andTarget:target];

    }

    return self;

}


//下面是布局的方法

-(void)makeUIWithTitle:(NSString*)title andLeftBtnImage:(NSString*)leftImage andleftBtnSelector:(SEL)leftSelector andRightBtnImage:(NSString*)rightImage andRightBtnSelector:(SEL)rightSelector andTarget:(id)target

{

    //导航条

    UIImageView *navView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    [self addSubview:navView];

    navView.image = [UIImage imageNamed:@"title_bg.png"];

    [navView release];

    //title

    UILabel *navLabel = [[UILabel alloc] initWithFrame:self.bounds];

    navLabel.text = title;//根据传过来的参数赋值

    navLabel.textColor = [UIColor whiteColor];

    navLabel.textAlignment = NSTextAlignmentCenter;

    navLabel.font = [UIFont boldSystemFontOfSize:22];

    [self addSubview:navLabel];

    [navLabel release];

    //左按钮

    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    leftBtn.frame = CGRectMake(10, 10, 57, 24);

    [leftBtn setImage:[UIImage imageNamed:leftImage] forState:UIControlStateNormal];

    [leftBtn addTarget:target action:leftSelector forControlEvents:UIControlEventTouchUpInside];

    if(leftImage)

    {

        [self addSubview:leftBtn];

    }

    //右按钮

    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    rightBtn.frame = CGRectMake(self.frame.size.width-31, 11.5, 21, 21);

    [rightBtn setImage:[UIImage imageNamed:rightImage] forState:UIControlStateNormal];

    [rightBtn addTarget:target action:rightSelector forControlEvents:UIControlEventTouchUpInside];

    if(rightImage)

    {

        [self addSubview:rightBtn];

    }

}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值