yassassin
yassassin

Reputation: 3195

UIToolbar precise size

I'm trying to customize my NavigationBar with the help of a toolbar. I've implemented it programmatically as follows:

UIToolbar* tools = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 100, 44.01)];

and then I added it to my NavigationBar. The problem is that I have this ugly effect on the borders:

navigationbar + toolbar

I've tried to change the y and the height values, with no results. Do you have any ideas to avoid this?

Thanks in advance, yassa

Upvotes: 2

Views: 4802

Answers (3)

Josip B.
Josip B.

Reputation: 2464

Or you can do it in this way. Just create new subclass of UIToolbar like this

   @interface MyToolbar : UIToolbar

    @end

    @implementation MyToolbar

    - (id)initWithFrame:(CGRect)frame {

        if ((self = [super initWithFrame:frame])) {

            self.opaque = NO;
            self.backgroundColor = [UIColor clearColor];
            self.clearsContextBeforeDrawing = YES;      
        }
        return self;
    }


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

    - (void)dealloc {
        [super dealloc];
    }

@end

And use it as normal UIToolbar. I don't know why but it just works.

Upvotes: 0

yassassin
yassassin

Reputation: 3195

I partially agree with previous answers and comments. The solution you suggested works fine for custom buttons. But what if I want to implement standard Edit button? Access to the standard buttons/icons is through the UIBarButtonItem class, not UIButton. And you can't add UIBarButtonItem objects to a UIView.

After many research on the web, I've found the solution that completely cover my requirement. The toolbar must be created in the following way:

UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 95.0f, 44.01f)];
tools.tintColor = self.navigationController.navigationBar.tintColor;
tools.barStyle = -1;

And this is the result:

my toolbar

Hope it helps! yassa

Upvotes: 2

shannoga
shannoga

Reputation: 19869

I wouldn't do it this way. You can achieve the same effect by adding a view with 2 buttons to the navigationItem.rightBarButtonItem. it is very simple:

// view that will hold the buttons
UIView* container = [[UIView alloc] init];

// create 1 button and add it to the container
UIButton* button = [[UIButton alloc] init........];
[container addSubview:button];


//create 2 button and add it to the container
button = [[UIButton alloc] init.........];
[container addSubview:button];


// now create a Bar button item
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:container];

// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = barButtonItem;

Upvotes: 2

Related Questions