5

我使用此代码将插图添加到backIndicator导航栏的图像中。但是,这仅适用于图像的垂直定位。我只能将图像向上或向下移动,但不能向左或向右移动。看起来左/右插图不起作用。我不确定可能是什么问题。

 UIEdgeInsets insets = UIEdgeInsetsMake(0, 20, 0, 0); //(20,0,0,0) works fine
 UIImage *backArrowImage = [[UIImage imageNamed:@"Back"] imageWithAlignmentRectInsets:insets];

 [[UINavigationBar appearance] setBackIndicatorImage:backArrowImage];
 [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backArrowImage];

我也试过这个:

  UIImage * backArrowImage =[[UIImage imageNamed:@"Back"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 0) resizingMode:UIImageResizingModeStretch];

如果这不可能,我是否需要返回添加自定义后退按钮?

4

5 回答 5

7

我已经尝试了很多方法,最后这对我有用,你可以重新绘制你的图片,执行以下操作:

UIImage *arrow = [UIImage imageNamed:@"Back_Normal"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(arrow.size.width+10, arrow.size.height), NO, 0); // move the pic by 10, change it to the num you want
[arrow drawAtPoint:CGPointMake(10, 0)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

然后使用 finalImage 作为你的 backIndicatorImage。

于 2016-07-06T03:43:03.577 回答
4

斯威夫特版本。仅用于图像返回 btn。保持滑动返回 iOS 7+ 的内置手势识别器。

func setBackBtnTarget(target: AnyObject?, action: Selector) {
    var backImg: UIImage = // Your img
    let leftPadding: CGFloat = 10
    let adjustSizeForBetterHorizontalAlignment: CGSize = CGSizeMake(backImg.size.width + leftPadding, backImg.size.height)

    UIGraphicsBeginImageContextWithOptions(adjustSizeForBetterHorizontalAlignment, false, 0)
    backImg.drawAtPoint(CGPointMake(leftPadding, 0))
    backImg = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    navigationController?.navigationBar.backIndicatorImage = backImg
    navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImg

    let backBtn: UIBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: target, action: action)
    navigationItem.backBarButtonItem = backBtn
  }
于 2016-08-22T17:48:56.533 回答
1

周家雷的延伸形式

extension UIImage {
    func translatedHorizontally(by constant: CGFloat) -> UIImage? {
        let newSize = CGSize(width: size.width + constant, height: size.height)
        let newPoint = CGPoint(x: constant, y: 0.0)

        UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
        draw(at: newPoint)
        let translatedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return translatedImage
    }
} 
于 2019-04-10T06:32:48.093 回答
0

尝试添加已经插入定位的图像,这样您就不需要在之后进行插入。这是一个黑客,但它应该工作

于 2016-01-05T04:48:54.127 回答
-1

斯威夫特 4.1 (Xcode 9.4.1)

如果不知道alignmentRectInsets,可以先看这篇博客



为了简化答案,我们需要对图像边缘插图进行一些调整。

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
    }
}

https://stackoverflow.com/a/32576128/6113158Stefan Kendall



这是如何使用它的示例。

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNavBar(with: backImage)
    }

    func setupNavBar(with backIcon: UIImage) {
        navBarLeftImageInsects = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()

        if let backImage = backIcon.withInsets(navBarLeftImageInsects) {
            navigationController?.navigationBar.backIndicatorImage = backImage
            navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage
            navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        }
    }
}
于 2018-09-10T02:28:15.727 回答