Reputation: 14904
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:
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]
Upvotes: 5
Views: 4711
Reputation: 3098
swift version of @Piyush Sharma ' Answer
actionBarWidth = self.navigationController?.navigationBar.frame.width as! CGFloat
actionBarHeight = self.navigationController?.navigationBar.frame.height as! CGFloat
actionBarView.frame = CGRect (x: 0, y: 0, width: actionBarWidth, height: actionBarHeight)
let emptySpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
emptySpace.width = -16
let leftBarBtn = UIBarButtonItem(customView: actionBarView)
self.navigationItem.leftBarButtonItems = [emptySpace,leftBarBtn]
Upvotes: 0
Reputation: 1891
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];
Upvotes: 9