Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LeftbarButtonItem remove padding?

Tags:

swift

I would like to use a logo as LeftbarButtonItem in an UINavigationController, is it possible to remove the left padding here?

Ill alreay tried:

    let logoView = UIView(frame: CGRectMake(-15, 0, 44, 44))
    logoView.backgroundColor = UIColor.darkGrayColor()

    var leftBarButtonItem: UIBarButtonItem = UIBarButtonItem(customView: logoView)

    self.navigationItem.setLeftBarButtonItem(leftBarButtonItem, animated: false)

But thats not working. Ill mean that space shown here in this picture:

enter image description here

Any ideas? Thanks in advance.

Here is the Swift Solution:

    let logoView = UIView(frame: CGRectMake(0, 0, 44, 44))
    logoView.backgroundColor = UIColor.darkGrayColor()

    let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
    negativeSpacer.width = -20;

    let leftBarButtonItem: UIBarButtonItem = UIBarButtonItem(customView: logoView)

    self.navigationItem.leftBarButtonItems = [negativeSpacer, leftBarButtonItem]
like image 533
derdida Avatar asked Oct 27 '15 22:10

derdida


1 Answers

Here's an example of removing the padding to the left of a custom left button item:

UIBarButtonItem *backButtonItem // Assume this exists, filled with our custom view

// Create a negative spacer to go to the left of our custom back button, 
// and pull it right to the edge:
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
    target:nil action:nil];
negativeSpacer.width = -5; 

// Note: We use 5 above b/c that's how many pixels of padding iOS seems to add

// Add the two buttons together on the left:
self.navigationItem.leftBarButtonItems = [NSArray 
    arrayWithObjects:negativeSpacer, backButtonItem, nil];
like image 102
Piyush Sharma Avatar answered Nov 10 '22 02:11

Piyush Sharma