Claus
Claus

Reputation: 5722

Modifying spacing between two right UIBarButtonItems

I want to place two UIBarButtonItems on the right side of my navigation bar. The main problem I'm facing is the spacing between the buttons (the spacing between the right most button and the border of the navigation view has been solved thanks to this post). This is the code I'm using

// add buttons
        let buttonEdit: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        buttonEdit.frame = CGRectMake(0, 0, 40, 40)
        buttonEdit.setImage(UIImage(named:"iconMap"), forState: UIControlState.Normal)
        buttonEdit.addTarget(self, action: "rightNavItemEditClick:", forControlEvents: UIControlEvents.TouchUpInside)
        var rightBarButtonItemEdit: UIBarButtonItem = UIBarButtonItem(customView: buttonEdit)


        let buttonDelete: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        buttonDelete.frame = CGRectMake(0, 0, 40, 40)
        buttonDelete.setImage(UIImage(named:"iconMap"), forState: UIControlState.Normal)
        buttonDelete.addTarget(self, action: "rightNavItemDeleteClick:", forControlEvents: UIControlEvents.TouchUpInside)

        var rightBarButtonItemDelete: UIBarButtonItem = UIBarButtonItem(customView: buttonDelete)


        let spaceFix: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
        spaceFix.width = -10 
 // add multiple right bar button items
 self.navigationItem.setRightBarButtonItems([spaceFix,rightBarButtonItemDelete,rightBarButtonItemEdit], animated: true)

If I try to place an additional spacer between the two button (like shown here) there is no apparent effect. I need to get the buttons a bit closer, how can I do that?

Upvotes: 2

Views: 1147

Answers (1)

matt
matt

Reputation: 534925

You don't really get any control over the spacing between UIBarButtonItems. You can change a UIBarButtonItem's width, but that's all. A UIButtonItem is not a UIView; it has no frame, so you can't say anything about its position. If you want finer control over the look of the interface, you'll have to use a UIBarButtonItem which itself consists of a custom view (init(customView:)). Now you're in the UIView world and you can set the frames of its subviews, which can be real buttons or whatever.

Upvotes: 2

Related Questions