Fork me on GitHub

自定义navigationbar

一般是在你需要添加自定义navigationbar的地方:

self.navigationController.navigationBar.hidden = YES;

UINavigationBar *customNavigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
UIImageView *navigationBarBackgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[customNavigationBar addSubview:navigationBarBackgroundImageView];
UINavigationItem *navigationTitle = [[UINavigationItem alloc] initWithTitle:title];
[customNavigationBar pushNavigationItem:navigationTitle animated:NO];
[navigationTitle release];
[navigationBarBackgroundImageView release];

[self.view addSubview:customNavigationBar];

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundColor:[UIColor clearColor]];
backButton.frame = CGRectMake(0, 0, 80, 40);
[backButton setImage:[UIImage imageNamed:@"ico_back.png"] forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:@"ico_back_highlighted.png"] forState:UIControlStateSelected]; 
[backButton addTarget:self action:@selector(backHome) forControlEvents:UIControlEventTouchUpInside];
//UINavigationItem *navigatorItem = [TTNavigator navigator].visibleViewController.navigationItem; 
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButton;
[backBarButton release];

继承UINavigationController来自定义navigationbar是比较复杂的

posted on 2012-02-28 19:44  pengyingh  阅读(7279)  评论(0编辑  收藏  举报

导航