一般导航控制器内容有三个属性分别有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;
}