AppDelegate.m 导航栏自定义:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes: @{
UITextAttributeTextColor: [UIColor colorWithRed:(56/255.0) green:(61/255.0) blue:(63/255.0) alpha:1],
UITextAttributeTextShadowColor: [UIColor grayColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:@"ProximaNova-Bold" size:15.0f]
}];
LViewController.m 导航栏自定义左/右按钮:
// Left Button
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
[leftButton addTarget:self action:@selector(openLeftMenu) forControlEvents:UIControlEventTouchUpInside];
[leftButton setFrame:CGRectMake(0, 0, 45, 44)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
// Right Button
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setImage:[UIImage imageNamed:@"location"] forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(openRightMenu) forControlEvents:UIControlEventTouchUpInside];
[rightButton setFrame:CGRectMake(0, 0, 45, 44)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
结果(标题不居中):
我尝试了很多技巧,但有人工作: