iOS 导航栏基础设置

557 阅读1分钟
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"测试";
    // 修改 状态栏+导航栏背景色
    [self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];
    // 导航栏文字颜色+字号
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18 weight:1], NSForegroundColorAttributeName:[UIColor greenColor]}];
    // 设置导航栏的背景图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"111"] forBarMetrics:UIBarMetricsDefault];
    // 自定义状态栏背景view 一般不会在这里做手脚,但是也不排除特殊情况使用,原因是由于状态栏区域上的控件是隐藏的,所以只要在状态栏区域被渲染了颜色,状态栏的背景颜色就跟着一起改变,从而改变了状态栏的背景
    UIView *statusBarView = [[UIView alloc]   initWithFrame:CGRectMake(0, -20,    self.view.bounds.size.width, 20)];
    statusBarView.backgroundColor = [UIColor purpleColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 50, 20)];
    label.backgroundColor = [UIColor orangeColor];
    label.textColor = [UIColor whiteColor];
    label.text = @"广告";
    [statusBarView addSubview:label];
    [self.navigationController.navigationBar addSubview:statusBarView];
    // 设置原点从导航栏之下开始 默认全屏
//    self.edgesForExtendedLayout = 0;
    /* 设置状态栏的样式:白色字 、黑色字
     在plist文件添加字段:View controller-based status bar appearance,并设置BOOL值为NO
     */
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    // 设置导航栏为透明,返回按钮和导航栏上的按钮标题控件依旧展示:注释掉self.edgesForExtendedLayout = 0; 不要对状态栏做自定义View
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];

    
    self.view.backgroundColor = [UIColor redColor];
    
    // Do any additional setup after loading the view, typically from a nib.
}