- 导航栏右侧的Done按钮怎么是蓝色的?
- 导航栏右侧按钮大小设置
- 导航栏标题按钮样式设置
- 导航栏设置颜色
- 导航栏变白不显示颜色?
…
iOS 13 之后使用新的API和类(UINavigationBarAppearance)来设置导航栏的标题大小,颜色,字体;导航栏的颜色;导航栏按钮样式。
#import "MMBaseNavigationController.h"
@interface MMBaseNavigationController ()
@end
@implementation MMBaseNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
/// Config the navigation bar appearance
if (@available(iOS 13.0, *)) {
UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
// Navigation bar background image
appearance.backgroundImage = [UIImage imageNamed:@"top"];
// Navigation bar title
appearance.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]};
// Config the done button appearance
UIBarButtonItemAppearance *doneAppearance = [[UIBarButtonItemAppearance alloc] init];
doneAppearance.normal.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor whiteColor]};
appearance.doneButtonAppearance = doneAppearance;
self.navigationBar.standardAppearance = appearance;
self.navigationBar.scrollEdgeAppearance = appearance;
}
else {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"top"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
}
@end
- standardAppearance 和 scrollEdgeAppearance
standardAppearance 和 scrollEdgeAppearance 分别是导航栏的标准样式和滚动样式。
什么是标准样式?其实就是静态显示的样式。
什么是滚动样式,就是当导航视图被present出来这种,这时候如果不设置scrollEdgeAppearance的话导航栏颜色就是白色的(iPhone上是这样,iPad的不会出现)。
- 在Controller中添加导航栏按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done:)];
注意
- self.navigationItem,不小心写成self.navigationController.navigationItem就错了,不是一回事。
- style:UIBarButtonItemStyleDone
设置图片
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 40, 40);
[button setImage:[UIImage imageNamed:@"back-icon"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;