For some reason the right button in the navigation bar is 16px from the right. I'd like to make the margin smaller. What's the proper way to do that?
self.btnDone = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *btnImgDone = [UIImage imageNamed:@"btn_small_default.png"];
self.btnDone.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15.0];
[self.btnDone setTitle:@"Done" forState:UIControlStateNormal];
[self.btnDone setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.btnDone setTitleShadowColor:[UIColor colorWithWhite:0 alpha:.2f] forState:UIControlStateNormal];
self.btnDone.titleLabel.shadowOffset = (CGSize){0,-1};
[self.btnDone setBackgroundImage:btnImgDone forState:UIControlStateNormal];
[self.btnDone setBackgroundImage:[UIImage imageNamed:@"btn_small_active.png"] forState:UIControlStateHighlighted];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.btnDone];
I had the same issue and the only way i found to fix it was to use the setImageEdgeInsets:
.
If you want to move your button to the right (for example : 5 pts or 10 px), add the following line to the button declaration :
UIEdgeInsets buttonEdges = UIEdgeInsetsMake(0, 5, 0, -5);
[self.btnDone setImageEdgeInsets:buttonEdges];
If you want to support iOS6 and iOS7 you can do this :
CGFloat xOffset;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f)
{
// for ios7
xOffset = 5.0f;
}
else
{
// ios6
xOffset = 2.0f;
}
UIEdgeInsets buttonEdges = UIEdgeInsetsMake(0, xOffset, 0, - xOffset);
[self.btnDone setImageEdgeInsets:buttonEdges];
Hope it helps !
Try to put a fixed space after that button:
// Fixed space
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:20.0];
self.navigationItem.rightBarButtonItems = @[fixedSpace, yourButton];
The whole code I've used is:
// Bar button
UIButton *loadButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 15, 34)];
VVdLoadImage *loadImage = [[VVdLoadImage alloc] initWithFrame:CGRectMake(0, 0, 15, 34)];
loadImage.backgroundColor = [UIColor clearColor];
loadImage.userInteractionEnabled = NO;
[loadButton addSubview:loadImage];
[loadButton addTarget:self action:@selector(loadCards) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *loadBarButton = [[UIBarButtonItem alloc] initWithCustomView:loadButton];
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:20.0];
self.navigationItem.rightBarButtonItems = @[fixedSpace, loadButton];
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