self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"#2295f2"];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn-menu-h"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(menuBtnClick)];


self.navigationItem.leftBarButtonItem.imageInsets = UIEdgeInsetsMake(0, -20, 0, 0); self.navigationItem.rightBarButtonItem.imageInsets = UIEdgeInsetsMake(0, -10, 0, 10);
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes = @{
NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#ffffff"],
NSFontAttributeName : [UIFont systemFontOfSize:15]
};
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{
NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#ffffff"],
NSFontAttributeName : navItermFont
} forState:UIControlStateNormal];
導覽列預設是半透明的,若不處理,實際呈現的顏色會和設計稿有落差。
這行程式碼能關閉半透明效果,但副作用是原點座標會往下移。
[UINavigationBar appearance].translucent = NO;
self.edgesForExtendedLayout = UIRectEdgeNone;
當系統導覽列滿足不了設計稿時,就得自己畫一個。下面的程式碼只示範標題與左右按鈕,實際可依設計加入任意控件。
@interface CustomNav : UIView
@property (nonatomic, strong) UIButton *buttonLeft;
@property (nonatomic, strong) UIButton *buttonRight;
- (id)initWithTitle:(NSString *)title;
@end
@implementation CustomNav
- (id)initWithTitle:(NSString *)title {
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 64);
// 標題
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.frame = CGRectMake(SCREEN_WIDTH / 4.0, 22, SCREEN_WIDTH / 2.0, 40);
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
titleLabel.font = [UIFont systemFontOfSize:17];
titleLabel.text = title;
[self addSubview:titleLabel];
// 左按鈕
self.buttonLeft = [UIButton buttonWithType:UIButtonTypeCustom];
self.buttonLeft.frame = CGRectMake(0, 22, 40, 40);
[self addSubview:self.buttonLeft];
self.buttonLeft.titleLabel.font = [UIFont systemFontOfSize:15];
self.buttonLeft.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
// 右按鈕
self.buttonRight = [UIButton buttonWithType:UIButtonTypeCustom];
self.buttonRight.frame = CGRectMake(SCREEN_WIDTH - 40, 22, 40, 40);
[self addSubview:self.buttonRight];
self.buttonRight.titleLabel.font = [UIFont systemFontOfSize:15];
self.buttonRight.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.backgroundColor = [UIColor colorWithHexString:@"#2295f2"];
}
return self;
}
@end