導航欄設(shè)置透明
UIImage *image = [[UIImage alloc] init];[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
? ? //去掉透明后導航欄下邊的黑邊
? ? [self.navigationController.navigationBar setShadowImage:image];
? ? self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
? ? self.navigationController.navigationBar.translucent = YES;
導航欄背景自定義满粗,去除下方的黑線
@property (nonatomic, strong) UIImageView * navBarHairlineImageView;
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
? ? if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
? ? ? ? return (UIImageView *)view;
? ? }
? ? for (UIView *subview in view.subviews) {
? ? ? ? UIImageView *imageView = [self findHairlineImageViewUnder:subview];
? ? ? ? if (imageView) {
? ? ? ? ? ? return imageView;
? ? ? ? }
? ? }
? ? return nil;
}
- (void)viewWillAppear:(BOOL)animated{
? ? _navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
? ? _navBarHairlineImageView.hidden = YES;
? ? [self.navigationController.navigationBar setBarTintColor:你的顏色];
? ? [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
? ? self.tabBarController.tabBar.hidden = YES;
? ? [self navButton];
}
- (void)viewWillDisappear:(BOOL)animated{
? ? _navBarHairlineImageView.hidden = NO;
? ? [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithWhite:1 alpha:0.8]];
? ? [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],UITextAttributeTextColor,nil]];
? ? self.tabBarController.tabBar.hidden = NO;
}
返回按鈕
#pragma mark - 定義導航的按鈕
- (void)navButton{
? ? UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
? ? leftButton.frame = CGRectMake(0, 0, 25, 25);
? ? [leftButton setImage:[UIImage imageNamed:@"back_white"] forState:0];
? ? [leftButton setBackgroundColor:[UIColor clearColor]];
? ? leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
? ? [leftButton addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
? ? UIBarButtonItem * tiem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
? ? UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? target:nil action:nil];
? ? negativeSpacer.width = -10;
? ? self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, tiem, nil];
? ? UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
? ? rightButton.frame = CGRectMake(0, 0, 25, 25);
? ? [rightButton setImage:[UIImage imageNamed:@"search.png"] forState:0];
? ? [rightButton setBackgroundColor:[UIColor clearColor]];
? ? rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
? ? [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
? ? UIBarButtonItem * tiemRight = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
? ? UIBarButtonItem *negativeSpacerRight = [[UIBarButtonItem alloc]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? target:nil action:nil];
? ? negativeSpacerRight.width = -10;
? ? self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacerRight, tiemRight, nil];
}
- (void)leftClick{
? ? [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightButtonClick{
}