1. 添加标题(以图片的形式添加标题)
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
2. 添加导航栏左侧的按钮
因为有 高亮图片所以要自定义一个UIButton,在把这个按钮赋值给 leftBarButtonItem
// 设置导航栏左边的按钮 UIButton *tagButton = [UIButton buttonWithType:UIButtonTypeCustom]; [tagButton setBackgroundImage:[UIImage imageNamed:@"MainTagSubIcon"] forState:UIControlStateNormal]; [tagButton setBackgroundImage:[UIImage imageNamed:@"MainTagSubIconClick"] forState:UIControlStateHighlighted]; tagButton.size = tagButton.currentBackgroundImage.size; [tagButton addTarget:self action:@selector(tagClick) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tagButton];
3. 打印类名 和 方法名
NSLog(@"%s", __func__)
可以写成
#define OSLogFunc NSLog(@"%s", __func__)
4. self.title 等于 “self.navigationItem.title + self.tabBarItem.title”
self.title = @"我的关注"; // 这一句话就相当于修改了两个地方的title.一个是导航栏的一个是tabBar的 self.navigationItem.title = @"我的关注"; self.tabBarItem.title = @"我的关注";
5. 设置导航栏右边的按钮
如果要是右边只有一个按钮,使用“rightBarButtonItem” 如果右侧有两个或者更多。使用 “rightBarButtonItems”
// 设置导航栏右边的按钮 UIButton *settingButton = [UIButton buttonWithType:UIButtonTypeCustom]; [settingButton setBackgroundImage:[UIImage imageNamed:@"mine-setting-icon"] forState:UIControlStateNormal]; [settingButton setBackgroundImage:[UIImage imageNamed:@"mine-setting-icon-click"] forState:UIControlStateHighlighted]; settingButton.size = settingButton.currentBackgroundImage.size; [settingButton addTarget:self action:@selector(settingClick) forControlEvents:UIControlEventTouchUpInside]; UIButton *nightModeButton = [UIButton buttonWithType:UIButtonTypeCustom]; [nightModeButton setBackgroundImage:[UIImage imageNamed:@"mine-moon-icon"] forState:UIControlStateNormal]; [nightModeButton setBackgroundImage:[UIImage imageNamed:@"mine-moon-icon-click"] forState:UIControlStateHighlighted]; nightModeButton.size = nightModeButton.currentBackgroundImage.size; [nightModeButton addTarget:self action:@selector(nightModeClick) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItems =
@[ [[UIBarButtonItem alloc] initWithCustomView:settingButton], [[UIBarButtonItem alloc] initWithCustomView:nightModeButton] ];