关于NavigationItem

先吐槽一下bing和百度,同样的搜索条件为什么和Google的结果差别这么大呢。

隐藏返回按钮的文字

废话不说直接上代码

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //隐藏返回按钮的文字
    NSArray *viewControllerArr = [self.navigationController viewControllers];
    long previousViewControllerIndex = [viewControllerArr indexOfObject:self] - 1;
    UIViewController *previous;
    if (previousViewControllerIndex >= 0) {
        previous = [viewControllerArr objectAtIndex:previousViewControllerIndex];
        previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                 initWithTitle:@""
                                                 style:UIBarButtonItemStylePlain
                                                 target:self
                                                 action:nil];
    }
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}

自定义返回按钮

@implementation UINavigationItem (Custom)
- (void) makeLeftBarButtonWithTarget:(id)target action:(SEL)action {

    UIImage * imageNormal = [UIImage imageNamed:@"back_27px"];


    // create your button
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.exclusiveTouch = YES;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button setImage:imageNormal forState:UIControlStateNormal];

    // set the frame of the button (better to grab actual offset of leftbarbuttonitem instead of magic numbers)
    button.frame = CGRectMake(-15.0, 0.0, imageNormal.size.width + 15, imageNormal.size.height);//+15 目的确保button的可点击范围
    button.transform = CGAffineTransformMakeTranslation(-10, 0); //防止图片因为居中而和系统默认的back位置不统一
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imageNormal.size.width, imageNormal.size.height)];
    [view addSubview:button];

    // set the barbuttonitem to be the view
    UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.leftBarButtonItem = barButtonItem;
}
@end

调用

 [self.navigationItem makeLeftBarButtonWithTarget:self action:@selector(actions:)];

你可能感兴趣的:(关于NavigationItem)