自定义UINavigationBar

虽然自定义UINavigationBar很简单,并不是什么难点,但是具体的过程,写的时候容易忘记,所以在这里补充一下:

这里面讲的是一种简单方法,就是先把UINavigationBar隐藏,然后在View上面添加一个自己新定义的UINavigationBar,从而实现自定义效果

- (void)initNavigationBar{

    [self.navigationController setNavigationBarHidden:YES];

    UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 32)];

    [bar setBackgroundImage:[UIImage imageNamed:@"zhuche_bar2.png"] forBarMetrics:UIBarMetricsDefault]; 

    UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:nil];

    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];

    [left setFrame:CGRectMake(0, 2, 28, 28)];

    [left setImage:[UIImage imageNamed:@"zhuche_back.png"] forState:UIControlStateNormal];

    [left addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:left];

    [item setLeftBarButtonItem:leftButton];

    [bar pushNavigationItem:item animated:NO];

    [self.view addSubview:bar];

}
- (void)back{

    [self.navigationController popViewControllerAnimated:YES];

}



 


你可能感兴趣的:(UINavigationBar,OC笔记)