Search code examples
uibarbuttonitemuitoolbar

Change the Space between UIBarButton Items to be zero?


I have CustomImage UIButtons as my UIBarButtonItems. The way the Graphic designer made the Images, they are made to butt up against each other and not have a 10px or so gap between them. Is there a way to remove the gap between the UIBarButtonItems when adding them to the UIToolBar?

Thanks


Solution

  • So Fiddling around, I was able to get something to work. Though I'm not 100% sure I understand why. If I pass in an Image that is wide, (Greater than 15+px) it works fine, through if I put in an Image that is 1px wide, I cannot get it to work. What I'm looking for is something like:

    <image> | <Image> | <image> |    <flex space>       | <image> | <Image> | <image>
    

    Though I didn't want to create the Images with the | in them, so I have a Image that is just the | separator. When adding that Button, I cannot get the gap between them to close. I fiddled with the frame and Inset values to no avail.

    This is what I have for the wider buttons:

    -(id)initWithTitle:(NSString *)title normalImage:(UIImage *)normalImage highlightedImage:(UIImage *)highlightedImage selectedImage:(UIImage *)selectedImage target:(id)target action:(SEL)action {
    
    button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:normalImage forState:UIControlStateNormal];
    [button setImage:highlightedImage forState:UIControlStateHighlighted];
    [button setImage:selectedImage forState:UIControlStateSelected];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0, 74, 48)];
    
    // This is the code that seemed to make it work 
    // Though not 100% sure why
    // - - - - -
    UIEdgeInsets buttonInset = UIEdgeInsetsMake(0, -5, 0, -5);
    button.contentEdgeInsets = buttonInset;
    [button sizeToFit];
    button.autoresizingMask = UIViewAutoresizingNone;
    // - - - - -
    
    buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 74, 17)];
    buttonLabel.opaque = NO;
    buttonLabel.backgroundColor = [UIColor clearColor];
    buttonLabel.font = [UIFont systemFontOfSize:12.0f];
    buttonLabel.text = title;
    buttonLabel.textAlignment = UITextAlignmentCenter;
    [button addSubview:buttonLabel];
    [buttonLabel release];
    
        // create a UI
    self = [super initWithCustomView:button];
    [self setStyle:UIBarButtonItemStyleBordered];
    
    return self;
    
    }