views:

423

answers:

2

I'm adding a couple of buttons to an already-existing NavigationController. The two buttons are added to a UIView, which is pushed onto the NavigationItem. The buttons stop and reload a UIWebView.

alt text

Problem is that there's a slight offset issue that is making it all look pretty ugly. I wish I could set the UIToolbar to transparent or clear background but that doesn't seem to be an option. Can't seem to use negative offsets either. I've got color matching, but if you look closely there's 1px or 2px of highlighting up top that's causing a visual mismatch and then a slight offset at the bottom.

Some relevant code below (based on this, inbound Googlers). What are my options to resolve this?

    // create a toolbar for the buttons
 UIToolbar* toolbar = [[UIToolbar alloc]
        initWithFrame:CGRectMake(0, 0, 100, 45)];
 [toolbar setBarStyle: UIBarStyleDefault]; 
 UIColor *colorForBar = [[UIColor alloc] initWithRed:.72 green:0 blue:0 alpha:0];
 toolbar.tintColor = colorForBar;
    [colorForBar release];

 //[toolbar setTranslucent:YES];

 // create an array for the buttons
 NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

 // create a standard reload button
 UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc]
           initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
           target:self
           action:@selector(reload)];
 reloadButton.style = UIBarButtonItemStyleBordered;
 [buttons addObject:reloadButton];
 [reloadButton release];

 // create a spacer between the buttons
 UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
          initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
          target:nil
          action:nil];
 [buttons addObject:spacer];
 [spacer release];

 // create a standard delete button with the trash icon
 UIBarButtonItem *stopButton = [[UIBarButtonItem alloc]
          initWithBarButtonSystemItem:UIBarButtonSystemItemStop
          target:self
          action:@selector(stopLoading)];
 stopButton.style = UIBarButtonItemStyleBordered;
 [buttons addObject:stopButton];
 [stopButton release];

 // put the buttons in the toolbar and release them
 [toolbar setItems:buttons animated:NO];
 [buttons release];

 // place the toolbar into the navigation bar
 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
             initWithCustomView:toolbar];
 [toolbar release];

Update: Alignment was fixed by suggestion by @Jacob below. Now, how to solve the highlighting mismatch?

alt text

+1  A: 

I've had this same exact issue before, and after some hacking around, I figured this out:

You need to set the height of the UIToolbar's frame to 44.01 for this to work.

EDIT: Re: highlighting mismatch -> You can try setting the backgroundColor to the colorForBar.

 UIToolbar* toolbar = [[UIToolbar alloc]
                          initWithFrame:CGRectMake(0, 0, 100, 44.01)];
 UIColor *colorForBar = [[UIColor alloc] initWithRed:.72 green:0 blue:0 alpha:0];
 toolbar.tintColor = colorForBar;
 toolbar.backgroundColor = colorForBar;
Jacob Relkin
Excellent solve for the alignment issue. Any suggestions on how to get the bevel/highlighting to match?
editor
@editor, what highlighting mismatch?
Jacob Relkin
Check out the updated image above. There's a 1px or 2px thin white highlight line at the top, and it turns to gray as soon as the UIToolbar ends (midway under the "M" in "PM). I'd like it to look like the rest of my NavBar. Both bar style's are set to "Default" (UIBarStyleDefault).
editor
A: 

Instead of putting the buttons into a toolbar, you might just make them children views of a parent UIView. You can position a button directly through its frame or center property, without use of spacers, etc.

Alex Reynolds
That's a bit over my head, but I'll look into this. Thanks as always Alex, you're very helpful.
editor
@Alex, I'm actually doing this exact thing in one of my apps in development. :)
Jacob Relkin
@Alex, the only issue with this approach is that you need to acquire your own images, you cannot use the built-in `UIBarButtonSystemItem` s
Jacob Relkin