iosswiftinsetsuiedgeinsets

iOS: Increase spacing to UIBarButtonItem


I made a custom UINavigationBar looks like this:[1]

Now I want to increase spacing between left and left bar button. I tried to do that this way using UIEdgeInsets this way:

self.navigationItem.leftBarButtonItem?.imageInsets = UIEdgeInsetsMake(0, 20, 0, 0)

Then it gets shrieked 2]:


Solution

  • You can add extra spacing item before button:

        let button = UIBarButtonItem(title: "<", style: .plain, target: self, action: "someSelector")
        let spacing = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        spacing.width = 20;
        navigationItem.leftBarButtonItems = [spacing, button]