NavigationItem常见属性及方法

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    self.view.backgroundColor = [UIColor whiteColor];
    //设置标题文字
    self.title = @"页面一";
    self.navigationItem.title = @"页面一";
    //back  为默认标题文字,默认文字不显示
   
    //navigationBar是navigationController共用的,navigationItem不是共用的,需每个页面单独设置
   
    //找到navigationItem:self.navigationItem
   
    //设置自定义标题头
    //设置自定义标题头时:位置无效,默认居中显示
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(800, 0, 200, 40)];
    label.backgroundColor = [UIColor redColor];
    label.text = @"页面";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor cyanColor];
    label.font = [UIFont systemFontOfSize:24.0];
    self.navigationItem.titleView = label;
   
    //设置左按钮、右按钮
    //文字类型
//    self.navigationItem.leftBarButtonItem   左按钮
//    self.navigationItem.rightBarButtonItem  右按钮
    //实例化  UIBarButtonItem
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithTitle:@"left" style:UIBarButtonItemStylePlain target:self action:@selector(buttonDown:)];
    //将实例化的UIBarButtonItem赋给leftBarButtonItem
    self.navigationItem.leftBarButtonItem = leftBar;
   
    //图片类型
    UIBarButtonItem *imageBar = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"itemImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonDown:)];
    //将实例化的UIBarButtonItem赋给rightBarButtonItem
    self.navigationItem.rightBarButtonItem = imageBar;
   
    //系统图片类
    /*
     1、UIBarButtonSystemItemAction  分享
     2、UIBarButtonSystemItemAdd  +
     3、UIBarButtonSystemItemBookmarks  书
     4、UIBarButtonSystemItemCamera  照相机
     .
     .
     .
     */
    UIBarButtonItem *systemImage = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(buttonDown:)];
    self.navigationItem.leftBarButtonItem = systemImage;
   
    //自定义按钮
    UIButton *customButton = [UIButton buttonWithType:UIButtonTypeSystem];
    customButton.frame = CGRectMake(0, 0, 40, 40);
    customButton.backgroundColor = [UIColor orangeColor];
    [customButton setTitle:@"btn" forState:UIControlStateNormal];
    [customButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *custom = [[UIBarButtonItem alloc]initWithCustomView:customButton];
    self.navigationItem.leftBarButtonItem = custom;
   
    //多个按钮
    //数组里的元素为UIBarButtonItem实例化出来的对象
    //放置多个按钮时:左右按钮不能同时用一个相同的数组
//    //左按钮时:按数组顺序由左向右展示
//    NSArray *arr = @[systemImage,custom];
//    self.navigationItem.leftBarButtonItems = @[systemImage,custom];
//    //右按钮时:按数组顺序由右向左展示
//    self.navigationItem.rightBarButtonItems = @[leftBar,imageBar];
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(20, 100, 280, 40);
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"push" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
   
}
//UIBarButtonItem方法带参数的写法
- (void)buttonDown:(UIBarButtonItem *)button{
    NSLog(@"UIBarButtonItem");
}

- (void)buttonClick:(UIButton *)button{
    SecondViewController *second = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:second animated:YES];
}

  

转载于:https://www.cnblogs.com/xiehan87/p/4758783.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值