I want to display Next Button just like Back Button but the rightBarButton does not touch to end of the screen like the back barButton.
let button = UIButton(type: .system)
button.setImage(UIImage(named: "ic_next_button"), for: .normal) // 22x22 1x, 44x44 2x, 66x66 3x
button.setTitle("Next", for: .normal)
button.sizeToFit()
button.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
button.titleLabel?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
button.imageView?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
As the previous answer pointed out, all you need to do is to add a negative fixed space to the left of your nextButton
and it will be pushed to the right.
This code creates a negative fixed width of 8 points (which seems enough in your case, but feel free to adapt as you need):
let negativeWidthButtonItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace,
target: nil,
action: nil)
negativeWidthButtonItem.width = -8
After creating this button, add it to the rightBarButtonItems array:
self.navigationItem.rightBarButtonItems = [negativeWidthButtonItem, nextButton]
Some other answers on StackOverflow also refer to the same solution:
reduce left space and right space from navigation bar left and right bar button item
How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With