6

I'm using self.navigationItem.rightBarButtonItems to setup my navigation bar items. However, for the bar buttons, i'm using a custom view(button). I observe there is spacing between buttons. How can I remove this?

Dunes Buggy
  • 1,779
  • 1
  • 21
  • 41

4 Answers4

13

i know its too late, but i solved it using following method of UIBarButtonItem use

[barbuttonitem setImageInsets:UIEdgeInsetsMake(0, -30, 0, -70)];
Mehmet Emre Portakal
  • 1,774
  • 21
  • 37
india
  • 131
  • 1
  • 3
4

I solved this by using storybord interface.I know you are using custom Bar,but this answer will useful for those who use stroybord.

1.Select the Bar item.

2.Select the Size Inspector.

Here you can find image Inset,using top,bottom AND left , right you can change the position of Bar Item.

jithin
  • 459
  • 8
  • 21
3

You can't remove it. You can work around it by creating a bar button item with a custom view, where that custom view has you custom buttons all added as subviews. In this way you can directly control the exact positioning.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Is it possible to just mess with the `contentEdgeInsets` properties of the buttons, as mentioned here? http://stackoverflow.com/a/3693957/88111 – Craig Otis Jul 04 '13 at 16:55
  • @CraigOtis it's possible that you could make it work but you're relying on Apple not choosing to change the spacing that they add (and the value of which you guessed). – Wain Jul 04 '13 at 16:56
  • That's a good point. Creating your own view containing the buttons, as you describe, is probably a more robust solution. – Craig Otis Jul 04 '13 at 16:58
2

Here is an example that how you can resolve this issue:

Create an extension of UIBarButton

extension UIBarButtonItem
{
    /** Create custom right bar button for reduce space between right bar buttons */ 
    func initRightButton(let imageNamed:String, let target:UIViewController, let selector:Selector) -> UIBarButtonItem {  
        let frame = CGRectMake(0, 0, 30, 30)
        //Create imageView
        let imageView = UIImageView(frame:frame)
        imageView.image = UIImage(named: imageNamed)
        //Create Button
        let button = UIButton(frame: frame)
        button.addTarget(target, action: selector, forControlEvents: .TouchUpInside)
        //Create View and add imageView and Button
        let view = UIView(frame: frame)
        view.addSubview(imageView)
        view.addSubview(button)
        return UIBarButtonItem(customView: view)
    }
}

In your class controller use the method customizeNavigationBar

func customizeNavigationBar()  {
    //Create custom right bar button chat for reduce space between right bar buttons
    let barButton1 = UIBarButtonItem().initRightButton("customImageNamed1", target: self, selector: customSelector)
    let barButton2 = UIBarButtonItem().initRightButton("customImageNamed2", target: self, selector: customSelector)
    self.navigationItem.rightBarButtonItems = [barButton1,barButton2]
}
Smittey
  • 2,475
  • 10
  • 28
  • 35
Ariel Antonio Fundora
  • 1,330
  • 15
  • 20