iOS 纯代码定制 UINavigationController导航栏 左按钮又按钮以及中间的标题

iOS 纯代码定制 UINavigationController导航栏 左按钮右按钮以及中间的标题

1.用xcode6 创建的项目,要用纯代码的话,首页要关闭自带storyboard 。



2.然后再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:@"1"]forBarMetrics:UIBarMetricsDefault];

    //3.设置根视图

    self.window.rootViewController = nav;

    //4、显示窗口

    [self.windowmakeKeyAndVisible];

 return YES;

}
3.在 ViewController 里面去定制导航栏左右按钮,以及中间标题

    //定制标题

    //方式1

    //    self.title = @"通讯录";

    //方式2

   UILabel *titleLab = [[UILabelalloc]initWithFrame:CGRectMake(0,20, 80, 44)];

    titleLab.text =@"通讯录";

    titleLab.textColor = [UIColorwhiteColor];

    titleLab.textAlignment =NSTextAlignmentCenter;

    self.navigationItem.titleView = titleLab;


    //定制右按钮

    //方式1:用系统自带的

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"下一页"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(Next:)];

    //方式2:自己定制

    UIButton *but = [UIButtonbuttonWithType:UIButtonTypeCustom];

    but.frame =CGRectMake(0,0, 60, 44);

    [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,0, 60, 44);

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

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

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

    self.navigationItem.leftBarButtonItem = barBut;





ps:每天进步一点点,做一个快乐的程序猿

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值