I create one UIToolbar with code and another with interface builder. But, found out the two toolbar having different left and right padding which shown below:
From Interface Builder:
From Code:
UIImage *buttonImage = [[UIImage imageNamed:@"button.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0]; UIButton *btnTest = [UIButton buttonWithType:UIButtonTypeCustom]; [btnTest setBackgroundImage:buttonImage forState:UIControlStateNormal]; [btnTest setTitle:@"Back" forState:UIControlStateNormal]; [btnTest.titleLabel setFont:[UIFont boldSystemFontOfSize:13]]; [btnTest setBackgroundImage:[imgToolbarButton stretchableImageWithLeftCapWidth:5 topCapHeight:0] forState:UIControlStateNormal]; [btnTest addTarget:self action:@selector(clearDateEdit:) forControlEvents:UIControlEventTouchUpInside]; btnTest.frame = CGRectMake(0.0, 0.0, 50, 30); UIBarButtonItem *btnTestItem = [[UIBarButtonItem alloc] initWithCustomView:btnTest]; [self.toolbar setItems:[NSArray arrayWithObjects:btnTestItem,nil]]; [btnTestItem release];
My question is how can I adjust the left and right padding of UIToolbar by code?
Update
I discovered this alignment issue only happen to UIBarButtonItem with customView of UIButton, the alignment is fine with UIBarButtonItem. Any idea what cause this or to resolve this.
The only solution I can think of right now is to manually set the frame.
I had the same issue, and there's a neat trick you can do with a UIBarButtonSystemItemFixedSpace, add one of these with a negative width before your first button and after your last button and it will move the button to the edge.
For example, to get rid of the right hand margin add the following FixedSpace bar item as the last item:
Update for iOS 11 up to 13
Swift
let negativeSeperator = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) negativeSeperator.width = 12
Width must be positive in Swift version
Objc
UIBarButtonItem *negativeSeparator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSeparator.width = -12;
The margins on the left and right are 12px.
Update for iOS7 - margins are 16px on iPhone and 20px on iPad!
You had three possible solutions:
Absolutely, the first solution seems better than other
UIBarButtonItem *negativeSeparator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSeparator.width = -12;
if you use swift, code will be look like this
var negativeSeparator = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) negativeSeparator.width = -12
unfortunately, all the solution can't be use in iOS 11
You can also see some suggestion on the Developer Forums, but after you look all the comment, you will find suggestion on this topic is to create a custom UINavigationBar subclass and do something complexity.
Oh my god, All i want to do is to change the padding, not the navigation bar!
Fortunately, we can do this with a trick in iOS 11!
if you don't know alignmentRectInsets, you can read this blog first
swift version
var customButton = UIButton(type: .custom) customButton.overrideAlignmentRectInsets = UIEdgeInsets(top: 0, left: x, bottom: 0, right: -x) // you should do this in your own custom class customButton.translatesAutoresizingMaskIntoConstraints = false;
objective-c version
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom]; customButton.overrideAlignmentRectInsets = UIEdgeInsetsMake(0, x, 0, -x); // you should do this in your own custom class customButton.translatesAutoresizingMaskIntoConstraints = NO;
swift version
var item = UIBarButtonItem(customView: customButton)
objective-c version
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:customButton]
set a positive value, not a negative value
swift version
var positiveSeparator = UIBarButtonItem(barButtonSystemItem:.fixedSpace, target: nil, action: nil) positiveSeparator.width = 8
objective-c version
UIBarButtonItem *positiveSeparator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; positiveSeparator.width = 8;
fixedSpace type UIBarButton item must be the first element in array.
swift version
self.navigationItem.leftBarButtonItems = [positiveSeparator, item, ...]
objective-c version
self.navigationItem.leftBarButtonItems = @{positiveSeparator, item, ...}
after you doing all of the steps, you will see your padding become smaller and type area seems correct!
If you find something wrong, please let me know! I will try my best to answer your question!
Before iOS 11, you should care about the device's screen width; if the screen is 5.5 inch, the negative is -12 pt, on other screens is -8pt.
If you use my solution on iOS 11, you don't need to care about the device screen, just set 8pt, You should care about item's position in navigation bar, left side or right side, this will affect your custom view's alignmentRectInsets
If you want to promise your tap area is larger than 44 * 44 , you can override method below
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{ CGSize acturalSize = self.frame.size; CGSize minimumSize = kBarButtonMinimumTapAreaSize; CGFloat verticalMargin = acturalSize.height - minimumSize.height >= 0 ? 0 : ((minimumSize.height - acturalSize.height ) / 2); CGFloat horizontalMargin = acturalSize.width - minimumSize.width >= 0 ? 0 : ((minimumSize.width - acturalSize.width ) / 2); CGRect newArea = CGRectMake(self.bounds.origin.x - horizontalMargin, self.bounds.origin.y - verticalMargin, self.bounds.size.width + 2 * horizontalMargin, self.bounds.size.height + 2 * verticalMargin); return CGRectContainsPoint(newArea, point); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With