设置导航控制器内容

一般导航控制器内容有三个属性分别有leftBarButtonItem,rightBarButtonItem,title属性或者方法设置

这里写图片描述

注意 1,导航控制器内容只能通过栈顶控制器设置内容2,导航控制器内容位置是系统决定而大小是自己决定

具体代码如下:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    //创建一个窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //创建一个UINavigationController的根控制器
    ViewController *vc = [[ViewController alloc] init];
    //创建UINavigationController并将ViewController设置为UINavigationController的根控制器
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
    //设置窗口的根控制器
    self.window.rootViewController = nc;
    //显示
    [self.window makeKeyAndVisible];



    return YES;
}

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor blueColor];

    self.navigationItem.title = @"导航栏内容只能有栈顶控制器设置";

    //使用系统定义按钮
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
    //设置左边按钮
    self.navigationItem.leftBarButtonItem = leftItem;

    //创建一个UIButton设置默认和点击时的图片
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:@"navigationbar_friendsearch"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"] forState:UIControlStateHighlighted];
    //注意点:导航控制器内容的位置是系统控制的内容是自己决定
    btn.frame = CGRectMake(10, 0, 35, 35);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    //设置自定义右边按钮
    self.navigationItem.rightBarButtonItem = rightItem;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值