I have a UINavigationItem
on my view controller, and I am trying to reduce the spacing between my two RightBarButtonItems
. Here is some of my code:
// Create two UIBarButtonItems
let item1:UIBarButtonItem = UIBarButtonItem(customView: view1)
let item2:UIBarButtonItem = UIBarButtonItem(customView: view2)
var fixedSpace:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
fixedSpace.width = -20.0
// Add the rightBarButtonItems on the navigation bar
viewController.navigationItem.rightBarButtonItems = [item2, fixedSpace, item1]
As can be seen, I am using a FixedSpace UIBarButtonItem
, but this is not changing the spacing for some reason. I have thought about subclassing either the UINavigationItem
or the UIBarButtonItem
so that I can set the spacing accordingly, but I couldn't seem to find any methods that I could override to change the spacing between items.
Any insight on how to solve this problem would be greatly appreciated!
Another way is just by changing the Left and Right Image Inset in IB.
Thanks to @Fogmeister's help, I figured out that the width of the
view1
andview2
objects, which areUIButtons
, was too large. That was why there was abnormal spacing between them. Here is my final code:I create the background image for the first
UIButton
and then use its size to create the frame for thatUIButton
. I perform the same actions for the secondUIButton
. Then, I createUIBarButtonItems
from the twoUIButtons
. After that, I create 26px of fixed space and then -7.0px of fixed space. The purpose of the former is to create a certain amount of space between the two buttons. The purpose of the latter is to move allUIBarButtonItems
over to the right. Then, I add all of theUIBarButtonItems
asrightBarButtonItems
in a particular order so I get the look that I want.It works great now! Thanks for all of the help, Fogmeister!
From the docs for UIBarButtonItem...
If you set the width to
-20.0
it will ignore it and use the standard width.What is it you are trying to achieve with a negative width anyway? I'm almost certain there will be a better way.