iOS 纯代码设置导航栏上创建左按钮右按钮

应该首先再appdelegate.m 里面添加代码

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


    //1、创建窗口

    self.window = [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];

    self.window.backgroundColor = [UIColorwhiteColor];

    

    //2.创建一个导航控制器并添加子视图

    UINavigationController *nav = [[UINavigationControlleralloc]initWithRootViewController:[ViewControllernew]];

    //nav.navigationBar.barTintColor = [UIColor redColor];

    [nav.navigationBarsetBackgroundImage:[UIImageimageNamed:@"00"]forBarMetrics:UIBarMetricsDefault];

    //3.设置根视图

    self.window.rootViewController = nav;

    //4、显示窗口

    [self.windowmakeKeyAndVisible];

       return YES;

}

其次在 ViewController.m文件里面去定制导航栏左右按钮,以及中间标题

- (void)viewDidLoad {

    [super viewDidLoad];

       //定制标题


    

    //方式2

    UILabel *titleLab = [[UILabelalloc]initWithFrame:CGRectMake(0,208044)];

    titleLab.text =@"通讯录";

    titleLab.textColor = [UIColorwhiteColor];

    titleLab.textAlignment =NSTextAlignmentCenter;

    self.navigationItem.titleView = titleLab;


    //定制右按钮

    UIButton *but = [UIButtonbuttonWithType:UIButtonTypeCustom];

    but.frame =CGRectMake(0,06044);

    [but setTitle:@"下一页"forState:UIControlStateNormal];

    [but addTarget:selfaction:@selector(Next:)forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem  *barBut = [[UIBarButtonItemalloc]initWithCustomView:but];

    self.navigationItem.rightBarButtonItem = barBut;

    //定制左按钮

    UIButton *but = [UIButtonbuttonWithType:UIButtonTypeCustom];

    but.frame =CGRectMake(0,06044);

    [but setTitle:@"<返回"forState:UIControlStateNormal];

    [but addTarget:selfaction:@selector(popView:)forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem  *barBut = [[UIBarButtonItemalloc]initWithCustomView:but];

    self.navigationItem.leftBarButtonItem = barBut;

}

//左按钮事件响应函数

-(IBAction)Next:(id)sender{



}

//右按钮事件响应函数

-(IBAction)popView:(id)sender{



}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值