How to horizontally position the backIndicatorImage in a UINavigationBar

I use this code to add an insert to the image backIndicatorfor the navigation bar. However, this only works for vertical image positioning. I can only move the image up or down, but not left or right. It looks like the left / right insert is not working. I am not sure what could be the problem.

 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];

I also tried this:

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

If this is not possible, do I need to return to adding a custom back button?

+4
source share
5 answers

, , , , , :

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.

+3

. 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
  }
+3

, . ,

0

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

Swift 4.1 (Xcode 9.4.1)

If you do not know alignmentRectInsets, you can read this blog first .



To simplify the answer, we need some settings for inserts around the edges of the image.
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
    }
}

fooobar.com/questions/2262248 / ... Stefan Kendall



And here is an example of how to use it.
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)
        }
    }
}
-1
source

Source: https://habr.com/ru/post/1622768/


All Articles