Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next Button like Navigation's Back Button

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)

enter image description here

like image 978
Nitesh Avatar asked Jun 11 '17 11:06

Nitesh


1 Answers

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]

like image 170
Bruno Guerios Avatar answered Oct 30 '22 16:10

Bruno Guerios