将后退按钮移动到右侧UINavigationController

时间:2014-09-17 14:16:07

标签: objective-c uinavigationcontroller uinavigationbar uibarbuttonitem

我需要将股票返回按钮向右移动几个像素,因为它向左到目前为止,当iPad在某些情况下根本无法看到按钮时。

我尝试了什么:

[self.navigationController.navigationItem.backBarButtonItem setImageInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
[self.navigationItem.backBarButtonItem setImageInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
[self.navigationController.navigationBar.backItem.backBarButtonItem setImageInsets:UIEdgeInsetsMake(0, 20, 0, 0)];

这可能吗?

1 个答案:

答案 0 :(得分:0)

由于某种原因,该解决方案无法在最新版本的iOS(11+)中运行。我为移动后退按钮图像所做的是一个扩展:

extension UIImage {
  func withInsets(_ insets: UIEdgeInsets) -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(
      CGSize(width: size.width + insets.left + insets.right,
             height: size.height + insets.top + insets.bottom),
      false,
      self.scale)
    let origin = CGPoint(x: insets.left, y: insets.top)
    self.draw(at: origin)
    let imageWithInsets = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return imageWithInsets
  }
}

然后,您可以像这样调用实现:

UINavigationBar.appearance().backIndicatorImage = UIImage(named: "icon-back")?.withInsets(UIEdgeInsets(top: 0, left: 10.0, bottom: 0, right: 0 ))