Adding UIToolbar with two UIBarButtonItem to a UINavigationBar: poor UIToolbar and what about iPhone 4
Asked Answered
P

4

8

I'm following the second tip from here. In this tip two UIBarButtonItems are put together in a UIToolbar. Finally, the UIToolbar is added to the UINavigationBar. Now to my problems:

1) A white line is on top of the UIToolbar. If I increase the size of the UIToolbar, the gradient is wrong. I'm using the following size for the UIToolbar:

    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 44.01)];

How can I get rid of the white line? See here: alt text

The problem is that there is a white instead of a grey line. If it would be grey, everything would be perfect.

2) What about the difference of the display size of iPhone 3 and iPhone 4? Do I have to check which iPhone is used and then double the size?

Edit:

The buttons are created like in the following example I took from the above mentioned website:

// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];

// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];

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

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

// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];

[buttons release];

// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];

@ tc.:

I tried to subclass UIToolbar.

// MyToolbar.h

#import <Foundation/Foundation.h>


@interface MyToolbar : UIToolbar {

}

@end

// MyToolbar.m

#import "MyToolbar.h"


@implementation MyToolbar

- (void)drawRect:(CGRect)rect {
    // do nothing
}

- (id)initWithFrame:(CGRect)aRect {
    if ((self = [super initWithFrame:aRect])) {
        self.opaque = NO;
        self.backgroundColor = [UIColor clearColor];
        self.clearsContextBeforeDrawing = YES;      
    }
    return self;
}

@end
Pothouse answered 24/9, 2010 at 16:55 Comment(0)
S
5

There's no guarantee that UIToolbar draws seamlessly inside a UINavigationBar; this might be responsible for the white line you're seeing.

You might be able to subclass UIToolbar so that it doesn't draw (i.e. override -drawRect: to not do anything).

Spleen answered 25/9, 2010 at 1:24 Comment(4)
I tried to subclass UIToolbar. Now I have a black background for my toolbar. Which functions I have to implement that I get the same gradient/style from the UINavigationBar.Pothouse
Hmmm. Try additionally setting self.opaque = NO, self.backgroundColor = [UIColor clearColor], and self.clearsContextBeforeDrawing = YES in initWithFrame: or initWithCoder: as appropriate.Spleen
Now I have overridden initWithFrame: (see my edited question). This seems to work at first sight. I never subclassed a UI element before. Do I have to be aware of something (things which won't work or something like this)?Pothouse
You have to be aware of how UIToolbar does its drawing. It doesn't seem to be documented, so you pretty much have to guess. It might change in future OS releases. However, the UIToolbar docs don't tell you not to subclass it, so you're reasonably safe.Spleen
C
0

1) I can't explain the white line. Curious that it's only above your buttons. How are the buttons created? Also, is there a reason you're setting the height to 44.01, rather than just 44? I'm not positive that the height you set is honored in any case, they may be forced to 44 (someone correct me if I'm wrong).

2) You don't have to do anything for iPhone 4, everything is scaled automatically.

Cincinnati answered 24/9, 2010 at 19:17 Comment(1)
The white line is only above my buttons because I put my buttons in a UIToolbar. Then I put this UIToolbar on the UINavigationBar. So the white line comes from the size/drawing of my UIToolbar. Yes, it does matter if 44.01 or 44. The height of 44.01 works pretty well (except of this white line). If I take 44 then I get more drawing artifacts. I can also take 50 for the height. Then the white line disappears, but the gradient is not the same as the navigation bar anymore. See my edited question for details about creating this artifact.Pothouse
I
0

Did you try adding those buttons not inside a container UIView and then adding it as an item but manipulating with UIBarButtonSystemItemFlexibleSpace? I mean adding each of those buttons as an independent item.

Irate answered 25/9, 2010 at 10:37 Comment(1)
Yes, I didn't used a UIView and I didn't add them as independent items. Instead, I'm creating a UIToolbar, create a array of buttons, add the buttons to the toolbar and finally add the toolbar to the navigation bar. See my edited question for details.Pothouse
R
0

An UIToolbar is designed to be used at the bottom of Iphone screen, so if you use somewhere else it will try to make an edge effect on top side. To avoid this the toolbar height should be 2 pixels bigger then Navbar height. Bu this time you will have a different side effect which causes an edge clear on the bottom side of the navbar. (The navbar in any case locates the rightbarbuttonitem as aligned in the middle)

Raymer answered 28/2, 2011 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.