自定义NavgationBar返回按钮


iOS  上UINavigationController视图压栈形式,可以在当前视图无限制push许多视图,然而一些会觉得自带的push按钮不够美观,而且当上的上一个页面title很长的时候,那个返回按钮就很长,怎样定义一个自己返回按钮呢?像第三幅图片一样。。。


  



需要push的视图里面定义一个事件

- (IBAction)pushVC:(id)sender {
    NavViewController *navVC = (NavViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"NavViewController"];
    navVC.title = NSLocalizedString(@"设置", nil);
    [self.navigationController pushViewController:navVC animated:YES];
}


替换返回按钮,定义自定义按钮

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    
    [backBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
     
	// Do any additional setup after loading the view.
}

-(void)doBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


源码下载地址:https://github.com/XFZLDXF/XFNavBarBtn



原创博客欢迎转载分享,请注明出处http://blog.csdn.net/duxinfeng2010


你可能感兴趣的:(菜鸟学iOS的笔记)