ios移动开发navigationItem自带的返回按钮设置成自定义图片或者不显示父视图的标题文字

来源:互联网 发布:java中什么是构造方法 编辑:程序博客网 时间:2024/06/05 19:44

UIBarButtonItem *item = [[UIBarButtonItemalloc] initWithTitle:@""style:UIBarButtonItemStylePlaintarget:nilaction:nil];//创建按钮并且设置文字为空,不显示文字

   UIImage* image = [UIImageimageNamed:@"返回.png"];

[item setBackButtonBackgroundImage:[imageresizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width,0, 0)]forState:UIControlStateNormalbarMetrics:UIBarMetricsDefault];//设置title的位置偏移到一个不可见的位置,达到隐藏的目的

  self.navigationItem.backBarButtonItem = item;


原理很简单,第一行加载图片,第二行以加载图片的宽度结合resizableImageWithCapInsets生成一个缩放时不会拉伸的新图片作为BackButtonBackgroundImage,再在第三行设置title的位置偏移到一个不可见的位置,达到隐藏的目的。另外,如果需要全部统一替换,也可以在appdidFinishLaunching里通[UIBarButtonItem appearance]全部统一替换!


0 0