0

Does anyone know how I can move a back button image to the edge of the screen?

This is what it looks like right now:

how it looks like right now

and this is how I want it to look:

how I want it to be

shim
  • 9,289
  • 12
  • 69
  • 108

2 Answers2

0

Try to change its insets:

self.navigationController?.navigationBar.backItem?.backBarButtonItem?.imageInsets = UIEdgeInsets(top: 0, left: -12, bottom: 0, right: 12)

and find values to move it right to the edge.

shim
  • 9,289
  • 12
  • 69
  • 108
Kstin
  • 659
  • 1
  • 4
  • 18
0

If you add custom view as a button and add as leftBarButtonItem it always takes default x (frame's start position) position for that view as defined for navigation bar item. You can add something like this to get your desired output:

class AnotherViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addBackButton()
    }
    

    func addBackButton() {
        
        let containerView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 200, height: 50)))
   
        let btnBack = UIButton(frame: CGRect(x: -25, y: 0, width: 45, height: 45))
        btnBack.setImage(UIImage(named: "dark_ic_back.png")?.withRenderingMode(.alwaysTemplate), for: .normal)
        btnBack.tintColor = .black
        btnBack.addTarget(self, action: #selector(self.backAction(_:)), for: .touchUpInside)
        containerView.addSubview(btnBack)

        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: containerView)
    }
    
    @objc func backAction(_ sender: UIButton) {
        
       self.navigationController?.popViewController(animated: true)
    }
}

Output:

screenshot

shim
  • 9,289
  • 12
  • 69
  • 108
Kishan Bhatiya
  • 2,175
  • 8
  • 14