IOS学习 NSNavigationController 自定义:左右按钮、中间文字、背景图片

这篇博客主要介绍了如何在 iOS 开发中使用 Objective-C 对 NSNavigationController 进行自定义,包括设置导航栏的左右按钮、中间文字以及背景图片。博主详细讲解了导航栏的隐藏与显示,并通过代码实例展示了如何创建和设置 UIBarButtonItem,以及自定义标题视图。此外,还涵盖了视图控制器间的跳转操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导航栏的隐藏与显示

self.navigationController.navigationBarHidden = NO //有效

[self.navigationController setNavigationBarHidden:NO animated:YES];//有效

self.navigationController.navigationBar.hidden = NO//无效


@implementation HomeViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(100,100, 200, 40)];

    button.layer.cornerRadius =8;

    button.backgroundColor = [UIColorgreenColor];

    [button setTitle:@"push"forState:UIControlStateNormal];

    [button addTarget:selfaction:@selector(pushVC)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];

    

    //定义导航栏上左侧按钮类型

    UIBarButtonItem *leftItem = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarkstarget:selfaction:@selector(study)];

   

    /*一个导航控制器控制若干个视图控制器

     *一个导航控制器控制包含一个NavigationBar和一个toolBar

     *NavigationBar按钮是一个NavigationItem

     *通过设置NavigationItem的属性,显示Item(在NavigationBar上)

     *NavigationItem不是由NavigationBar控制,也是由NavigationController控制,而是由视图控制器控制

     */

    

    //错误的写法

//    self.navigationController.navigationItem.leftBarButtonItem = leftItem;

    self.navigationItem.leftBarButtonItem = leftItem;

    

    //设置导航栏上右侧控件属性

    UIButton *item = [[UIButtonalloc]initWithFrame:CGRectMake(100,100, 60, 30)];  //x,y设置无用,可设置宽,高

    item.layer.cornerRadius =8;

    item.backgroundColor = [UIColorgreenColor];

    [item setTitle:@"test"forState:UIControlStateNormal];

    [item addTarget:selfaction:@selector(Test)forControlEvents:UIControlEventTouchUpInside];

   

    UIBarButtonItem *rightItem = [[UIBarButtonItemalloc]initWithCustomView:item];

    self.navigationItem.rightBarButtonItem = rightItem;

    

    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, 100, 40)];

    label.text = @"首页";

    label.textAlignment =NSTextAlignmentCenter;

    self.navigationItem.titleView = label;    

}


-(void)study{

    UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"要继续学习吗"delegate:selfcancelButtonTitle:@"不要"otherButtonTitles:@"",nil];

    [alertView show];

}


-(void)Test{

    UIActionSheet *ActionSh = [[UIActionSheetalloc]initWithTitle:@"写字吗"delegate:selfcancelButtonTitle:@"不写"destructiveButtonTitle:@"重写"otherButtonTitles:@"写第一节",@"写第二节",nil];

    [ActionSh showInView:self.view];

}


-(void)pushVC{

    UIViewController *secondVC = [[SecondViewControlleralloc]init];

    secondVC.view.backgroundColor = [UIColorcyanColor];

    //跳转至子页面

    [self.navigationControllerpushViewController:secondVC animated:YES];

}



@implementation SecondViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(100,100, 200, 40)];

    button.layer.cornerRadius =8;

    button.backgroundColor = [UIColorpurpleColor];

    [button setTitle:@"second"forState:UIControlStateNormal];

    [button addTarget:selfaction:@selector(backVC)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];

    

    UIButton *button2 = [[UIButtonalloc]initWithFrame:CGRectMake(100,200, 200, 40)];

    button2.layer.cornerRadius =8;

    button2.backgroundColor = [UIColororangeColor];

    [button2 setTitle:@"push"forState:UIControlStateNormal];

    [button2 addTarget:selfaction:@selector(pushVC3)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button2];

    

    UIBarButtonItem *rightItem = [[UIBarButtonItemalloc]initWithTitle:@"自定义"style:UIBarButtonItemStyleBorderedtarget:selfaction:nil];

    

    self.navigationItem.rightBarButtonItem = rightItem;

    

    UIBarButtonItem *leftItem = [[UIBarButtonItemalloc]initWithTitle:@"root"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(backVC1)];

    self.navigationItem.leftBarButtonItem = leftItem;

    

    UIImageView *titleView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, 100,44)];

    titleView.image = [UIImageimageNamed:@"button_ok"];

    self.navigationItem.titleView = titleView;

}


-(void)backVC1{

    [self.navigationControllerpopViewControllerAnimated:YES];


}


-(void)backVC{

    //导航栏隐藏

    if (self.navigationController.toolbarHidden) {

        [self.navigationControllersetToolbarHidden:NOanimated:YES];

        [self.navigationControllersetNavigationBarHidden:NOanimated:YES];

    }else

    {

        [self.navigationControllersetToolbarHidden:YESanimated:YES];

        [self.navigationControllersetNavigationBarHidden:YESanimated:YES];

    }

}


-(void)pushVC3{

    UIViewController *threeVC = [[ThreeViewControlleralloc]init];

    threeVC.view.backgroundColor = [UIColoryellowColor];

    //跳转至子页面

    [self.navigationControllerpushViewController:threeVC animated:YES];    

}


接跳传页面的threeView

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值