Programmatically UIToolbar on the bottom

Mc.Lover picture Mc.Lover · Nov 28, 2011 · Viewed 19.6k times · Source

I am creating a UIToolbar programmatically but the issue is the position of this tool bar is up (navigation bar position). how can I put it automatically on the bottom?

Here is my code:

    CGRect rect2 = CGRectMake(0, toolBar.frame.origin.y , self.view.frame.size.width , 0);
    toolBar = [[UIToolbar alloc]initWithFrame:rect2];
    toolBar.barStyle = UIBarStyleBlackTranslucent;
    [toolBar sizeToFit];
    [self.view addSubview:toolBar];
    [toolBar release];

because my application is universal and my view controller class does not have any nib file I need to define it for both iPad and iPhone, and I don't want use UIUserInterfaceIdiomPad.

Answer

Jasarien picture Jasarien · Nov 28, 2011

You're setting rect2's y position to the value of [toolbar frame].origin.y, which at that point in the code is either nil, or pointing to some other instance of a toolbar, because you then immediately afterwards alloc and init a new toolbar.

Even if the toolbar was valid when you set it's frame, you can't use it's current y value as the new y value, because it will be 0.

You should position it relative to the bottom of the screen, minus the height of the toolbar. Try this instead:

CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 44);