OC_导航栏 + iOS10

导航栏标题
// 当标题比较复杂的时候
// 自定义视图
UIView * titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
self.navigationItem.titleView = titleView;  
导航栏
// 设置是否透明
self.navigationController.navigationBar.translucent = NO;
// 导航栏背景图片  
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];  
// 导航栏标题的颜色,自定义标题的时候可以不使用  
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];  
// 导航栏颜色(透明时候会有透明效果)
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
// 导航栏背景色  
self.navigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"475478"];  
// iOS10
    [self.navigationController.navigationBar.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (iOS10) {
            
            if ([obj isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {
                obj.subviews.firstObject.hidden = YES;
            }
        } else {
            if ([obj isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {
                obj.subviews.firstObject.hidden = YES;
            }
        }
    }];
// 导航栏阴影
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init]; 
// 功能按键  
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"backIcon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backtoself)];  

    // 自定义功能按键
    // 调整间距
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -5.0;
    // 返回
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    backBtn.frame = CGRectMake(0, 0, 22, 22);
    [backBtn setImage:[_IMG(@"backIcon") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backtoself) forControlEvents:UIControlEventTouchUpInside];
    // 关闭
    UIButton * closeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    closeBtn.frame = CGRectMake(0, 0, 44, 22);
    [closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
    closeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem * closeItem = [[UIBarButtonItem alloc] initWithCustomView:closeBtn];
    self.navigationItem.leftBarButtonItems = @[backItem,closeItem];

你可能感兴趣的:(OC_导航栏 + iOS10)