7

I have added a custom button to the navigation bar's custom right bar button item as shown in the image.

I'm want to be able to remove the space after the button so that it touches the right edge of the screen but couldn't find a solution even after searching. If someone could mention how, would be great.

This is the code I'm using

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
aButton.frame = CGRectMake(50.0, 0.0, 60, 44);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:@selector(testButtonControl:) forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItem = aBarButtonItem;

enter image description here

carbonr
  • 6,049
  • 5
  • 46
  • 73

4 Answers4

17

You can add a fixed space element with a negative width (I know, this sounds like a hack, but it works):

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
aButton.frame = CGRectMake(50.0, 0.0, 60, 44);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:@selector(testButtonControl:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *spaceFix = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
spaceFix.width = -5;

self.navigationItem.rightBarButtonItems = @[spaceFix, aBarButtonItem];
Tom
  • 5,068
  • 5
  • 29
  • 41
  • Too bad it seems you cannot set a negative width in Interface Builder in Xcode 5. – TalkLittle Mar 13 '14 at 16:14
  • This worked perfectly. When I was adding a right UIBarButtonItem with a custom view, it leaved a weird space right to it... I can't tell why – Moisés Olmedo Apr 05 '14 at 03:35
  • any way to use the negative space to decrease the space between the buttons? They seem to not be affected if I place another of it between them – Claus Feb 05 '15 at 01:17
3

You have to create a new view, add button and everything else you want and add it to the titleView as shown.

- (void)viewDidLoad {
    self.navigationItem.titleView = [self titleView];
}

- (UIView *)titleView {
    CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
    CGFloat width = 0.95 * self.view.frame.size.width;
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, navBarHeight)];
..
// please add what you need here
....
}
carbonr
  • 6,049
  • 5
  • 46
  • 73
0

You can't, unless you subclass UINavigationBar or override its methods via category.

Kyr Dunenkoff
  • 8,090
  • 3
  • 23
  • 21
  • Can you explain how to after I subclass it or after i use category. I know how to do both but I don't know how to do this. – carbonr Aug 05 '12 at 14:08
0

You can do this by adding a UIView then add inside it your button with x=spaceWidth y = 0.

look at the below code for the right item (this is inside UINavigationItem category)

1 -

UIView *_rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)]; [_rightView setBackgroundColor:[UIColor purpleColor]];//debug color

2-

    UIButton *aLeftButton = [[UIButton alloc] init];

   //...//

    aLeftButton.frame = CGRectMake(-15, 0, 44, 44);//in your case +15
    [aLeftButton setBackgroundImage:[UIImage imageNamed:buttonName] forState:UIControlStateNormal];
    [aLeftButton setBackgroundImage:[UIImage imageNamed:[buttonName stringByAppendingString:@"Selected"]] forState:UIControlStateSelected];
    [aLeftButton setAdjustsImageWhenDisabled:NO];
    [aLeftButton setAdjustsImageWhenHighlighted:NO];

    [aLeftButton addTarget:theSender action:aSelector forControlEvents:UIControlEventTouchUpInside];
    [_rightView addSubview:aLeftButton];
    UIBarButtonItem *aBarButton = [[UIBarButtonItem alloc] initWithCustomView:_rightView];
    [leftBarButtonsItem addObject:aBarButton];`

3

self.leftBarButtonItem = leftBarButtonsItem;

Bobj-C
  • 5,276
  • 9
  • 47
  • 83