关于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:)];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,641评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 月光将灯影拉长 古老的城墙静静守望 阳光照射不到的地方 寂寞在腐烂中肆意生长 凝固的时光,为谁苍凉 青石板上落满秋...
    水木宁阅读 665评论 14 20
  • 昼短夜忙的日子, 我是纤臂当车的虫子。 花开叶落的房子, 我是不识五谷的傻子。 觥斛交错的桌子, 我是人去盘空的影...
    陈在阅读 121评论 0 5
  • 绿色的粽叶散发着淡淡的清香, 包裹着粘粘的糯米。 糯米紧紧团成一团, 好像一家人。 糯米中露出了那香甜的枣子。 绿...
    小yoyo阅读 833评论 0 1