Programmatically UIToolbar on the bottom
Asked Answered
C

3

7

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.

Coauthor answered 28/11, 2011 at 13:12 Comment(6)
Your question is confusing. Could you modify it to show a screenshot of what you mean? And perhaps add a little more code to show how you are creating the toolbar?Farquhar
toolBar.frame = CGRectMake(0, 436 , 320 , 44);Hyder
up means navigation bar position !Coauthor
You can not refer to the toolbars frame before you even have created it. You need to first create it then move it, or set the CGRectMake to the correct possition right away. Like CGRectMake(0, 0, 320, 480 - 44)Digitize
because my application is universal and my viewController class does not have any nib file I need to define it for both iPad and iPhone I don't want use UIUserInterfaceIdiomPadCoauthor
See my answer here: #36055658Drews
A
11

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);
Ake answered 28/11, 2011 at 14:39 Comment(2)
Nice. I've been using view.bounds which puts the toolbar about 20 pixels above the bottom. This does the trick.Contaminate
@Ake Hi, I have a similar situation but in Swift. However, your solution is not working even though it's a direct translation. The toolbar appears for other height values, but not this one. let toolbar = UIToolbar(frame: CGRectMake(0, UIScreen.mainScreen().bounds.size.height - 60, UIScreen.mainScreen().bounds.size.width, 60));Ticker
F
9

This creates a bottom aligned toolbar

CGRect frame, remain;
CGRectDivide(self.view.bounds, &frame, &remain, 44, CGRectMaxYEdge);
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:frame];
[toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
[self.view addSubview:toolbar];
Fairman answered 28/4, 2013 at 19:6 Comment(1)
Good answer but when using this with table view, first cell appears below navigation bar and last cell appears below toolbar.Parodic
L
0
CGRect rect2 = CGRectMake(0, toolBar.frame.origin.y , self.view.frame.size.width , 0);

[toolbar setFrame:rect2];

try this i hope it may help you

Lutist answered 28/11, 2011 at 13:16 Comment(1)
no I set my rect position I did not write my whole code sorry !Coauthor

© 2022 - 2024 — McMap. All rights reserved.