1

我正在尝试在我的应用程序中实现自定义后退按钮。我不想要一个“后退”的头衔或任何前一个 VC 的头衔。相反,我希望它看起来像所有 VC 的附加图像。

我尝试在 UIBarButtonItem 的外观代理上使用 setBackButtonBackgroundImage 来替换图像,但它仍然显示“后退”标签,我不知道如何摆脱它。

有没有人对实现这一点的最佳实践有任何建议?我需要某种 UINavigationController 子类吗?或者我是否仍然需要使用我过去见过的人们使用的自定义返回方法路由(似乎很hacky)去setLeftBarButtonItem?任何帮助将不胜感激。

自定义后退按钮图像

4

2 回答 2

0

尝试将 UIButton 作为子视图放在 leftBarButtonItem 中,并带有一个动作和你想要的图像,一切都会好起来的 :) (你可能需要稍微编辑它们,但这不应该是火箭科学 :P)

于 2013-01-28T11:21:35.913 回答
0

在我的许多项目中,我使用了一个自定义的后退按钮,它只是一个没有文本的图标。我做了一些实验以找到最好的方法并提出了以下方法(我并不是说它是最好的方法或唯一的方法,但它对我有用并且稳定)。

我的想法是实现一个 Root ViewController 来重新设置标准后退按钮的样式。事实证明这根本不容易。在摆弄了大约 1 天的不同选项后,我终于找到了一个可行的解决方案。

诀窍是要有一个足够大的透明背景(比实际的背面 V 形更大,否则它会被调整为更小的尺寸),然后为 V 形使用自定义图像。

它有效,但正如您在下面的测量结果中看到的那样,它有一个小缺点:左侧有额外的 14 点间隙。如果您想在右侧匹配按钮,则需要将其补偿 14 个点(是的,它的点在视网膜上是 28 个像素...)

在此处输入图像描述

这是我的代码:

//this vc implements a custom back button, you can make this a root controller
//from which you inherit all your view controllers. But for simplicity reasons for this explanation
//I skipped this

class VCRoot: UIViewController {

 override func viewDidLoad() {
  //call supers
  super.viewDidLoad()
  //create custom back item
  let backItem = UIBarButtonItem()
  //as image set the back chevron icon its a 22x22 points (so 44x44 in retina)
  backItem.image=PaintCode.imageOfBarBtnBack
  let b = PaintCode.imageOfBarBtnBackgroundEmpty
  //as background I use a 1 = 33 points (so 2x66 retina) fully transparant image
  backItem.setBackButtonBackgroundImage(b, forState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
  //set the newly created barbuttonitme as the back button
  navigationItem.backBarButtonItem=backItem
 }
}

//this vc puts a "forwards" button in the navbar on the right with a matching arrow
//The image of this matching forward arrow is the correct size (width!) so that its the same
//distance from the edge of the screen as the back button
 
class VC2: UIViewController {
 
 //outlet to the barbutton item from IB
 @IBOutlet weak var barbtn: UIBarButtonItem!
 
 override func viewDidLoad() {
  //call supers
  super.viewDidLoad()
 //the forward chevron in the image is shifted 14 points (so 28 retina) to the left 
 //so it has same distance from edge => its (22+14) x 22 = 36 x 22 points (72 x 44 retina) 
  barbtn.image=PaintCode.imageOfBarBtnForward
 }
}

您也可以在这里参考我的博客: http ://www.hixfield.net/blog/2015/05/adding-a-custom-back-button-the-standard-way/

于 2015-06-07T13:43:53.927 回答