GithubHelp home page GithubHelp logo

backbarbuttonitem's Introduction

设置BackBarButtonItem为指定图片

iOS7之后,系统可以全局设置返回按钮,并且支持右滑返回上一级页面手势,但是如果想要自定义返回按钮的图片是有点麻烦的。下面介绍一种我个人认为比较简单高效的方法。先上效果图:
效果图
原理比较简单,由于设置BackBarButtonItem的背景图的时候,BackBarButtonItem的title会显示出来,即使把title设置为空字符串,还有一个箭头仍然会显示出来,好在苹果提供了方法可以设置title偏移,这样只需要把这个title偏移到屏幕之外看不到的地方就可以了。主要代码:

UIImage *backButtonImage = [[UIImage imageNamed:@"iconfont-fanhui"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -backButtonImage.size.height*3) forBarMetrics:UIBarMetricsDefault];

在设置图片的时候可能会出现图片拉伸的问题,如图:

解决这个问题可以用到苹果提供的九切片技术(不了解的同学可以网上查一下),设置图片空白部分为拉伸,返回箭头为原图显示就可以了。

如果对返回按钮的图片没有特殊要求,只是显示一个箭头的话,完全不需要使用这种方法,只需要设置BackBarButtonItem的title为空就可以:

UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = @"";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//设置返回箭头颜色

效果如图:

backbarbuttonitem's People

Contributors

zyfoolboy avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.