Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigationItem.rightBarButtonItem margin in iOS7

Tags:

ios7

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];

enter image description here

like image 532
Alex L Avatar asked Sep 21 '13 16:09

Alex L


2 Answers

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 !

like image 133
Rom. Avatar answered Sep 28 '22 05:09

Rom.


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];
like image 41
RFG Avatar answered Sep 28 '22 07:09

RFG