Skip to content

Instantly share code, notes, and snippets.

@JeOam
Last active February 21, 2016 12:51
Show Gist options
  • Save JeOam/2d833f7736db712486cd to your computer and use it in GitHub Desktop.
Save JeOam/2d833f7736db712486cd to your computer and use it in GitHub Desktop.
NavigationBar template

隐藏导航栏:

[self.navigationController setNavigationBarHidden:YES];

顶部导航左边按钮:

    UIButton *leftNaviBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftNaviBtn.frame = CGRectMake(0, 0, 44, 44);// 设置不了位置属性,可以设置大小属性
    [leftNaviBtn setBackgroundImage:[UIImage imageNamed:@"btn_more"] forState:UIControlStateNormal];
    [leftNaviBtn addTarget:self action:@selector(clickOnLeftNavigationItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftNaviBtn];
    self.navigationItem.leftBarButtonItem = leftItem;

设置顶部导航的背景颜色:

self.navigationController.navigationBar.barTintColor = COLOR_WITH_RGBA(512, 124, 200, 1);
[self.navigationController.navigationBar setTranslucent:NO];

设置顶部导航 title 颜色为白色:

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

设置导航栏上,按钮(如返回按钮)的颜色:

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
@JeOam
Copy link
Author

JeOam commented Jun 11, 2014

设置导航栏为全透明,但保留导航按钮的显示:

@property (strong, nonatomic) UIImage *navBGImage;
@property (strong, nonatomic) UIImage *navShadowImage;
@property (assign, nonatomic) BOOL navTranslucent;
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:self.navBGImage forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = self.navShadowImage;
    self.navigationController.navigationBar.translucent = self.navTranslucent;
}

- (void)setupUI{
    self.navTranslucent = self.navigationController.navigationBar.translucent;
    self.navBGImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
    self.navShadowImage = self.navigationController.navigationBar.shadowImage;

    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
}

@JeOam
Copy link
Author

JeOam commented Jun 11, 2014

@JeOam
Copy link
Author

JeOam commented Jul 21, 2014

Hide 'Back' button on navigation bar:

self.navigationItem.hidesBackButton = YES;

@JeOam
Copy link
Author

JeOam commented Jul 26, 2014

Two UIBarButtonItems as rightBarButtonItem

cameraButton = [[UIBarButtonItem alloc] initWithTitle:@"Camera" style:UIBarButtonItemStylePlain target:self action:@selector(cameraButton)];
writingButton = [[UIBarButtonItem alloc] initWithTitle:@"Text" style:UIBarButtonItemStylePlain target:self action:@selector(writingButton)];
NSDictionary *textAttributesBalck = [NSDictionary dictionaryWithObjectsAndKeys:
                                     [UIColor blackColor], NSForegroundColorAttributeName,
                                     nil];
self.navigationItem.rightBarButtonItems = @[writingButton, cameraButton];

@JeOam
Copy link
Author

JeOam commented Sep 22, 2014

Programmatically get height of navigation bar

NSLog(@"Navframe Height=%f", self.navigationController.navigationBar.frame.size.height);

@JeOam
Copy link
Author

JeOam commented Apr 27, 2015

NavigationBar 微调颜色:CRNavigationController

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment