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 I
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!
Just add negative constants to your leading and trailing constraints of the UIToolbar
. That way, the toolbar starts and ends outside of your view and therefore has less padding.
I met the same issue. Finally, I solved this by subclassing UIToolbar and override the layout subviews method.
- (void)layoutSubviews {
[super layoutSubviews];
if (leftItem_ && leftItem_.customView
&& [leftItem_.customView isKindOfClass:[UIButton class]]) {
CGRect newFrame = leftItem_.customView.frame;
newFrame.origin.x = 0; // reset the original point x to 0; default is 12, wired number
leftItem_.customView.frame = newFrame;
}
if (rightItem_ && rightItem_.customView
&& [rightItem_.customView isKindOfClass:[UIButton class]]) {
CGRect newFrame = rightItem_.customView.frame;
newFrame.origin.x = self.frame.size.width - CGRectGetWidth(newFrame);
rightItem_.customView.frame = newFrame;
}
}
I stumbled upon this when trying to center a toolbar as a subview of an UIButton. As the toolbar needed to be of small width, the best solution was a flexible button on each side:
UIBarButtonItem flexibleButtonItemLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem centeredButtonItem = ...
UIBarButtonItem flexibleButtonItemRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIToolbar toolbar = ...
toolbar.items = @[flexibleButtonItemLeft, centeredButtonItem, flexibleButtonItemRight];
use this button
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
I think a tricky way is to use Aspects (or method swizzling),as blew:
[UINavigationBar aspect_hookSelector:@selector(layoutSubviews) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info){
UINavigationBar *bar = info.instance;
//[bar layoutSubviews];
if ([bar isKindOfClass:[UINavigationBar class]]) {
if (@available(iOS 11, *)) {
bar.layoutMargins = UIEdgeInsetsZero;
for (UIView *subview in bar.subviews) {
if ([NSStringFromClass([subview class]) containsString:@"ContentView"]) {
UIEdgeInsets oEdges = subview.layoutMargins;
subview.layoutMargins = UIEdgeInsetsMake(0, 0, 0, oEdges.right);
}
}
}
}
} error:nil];