UIToolbar programmatically below

I am creating UIToolbar program code, but the problem is that the position of this toolbar is up (the position of the navigation bar). How can I put it automatically to 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 visibility controller class does not have any nib file that I need to define for iPad and iPhone and I don't want to use UIUserInterfaceIdiomPad .

+6
source share
3 answers

You set the position of rect2 y to the value [toolbar frame].origin.y , which at this point in the code is either nil or points to some other instance of the toolbar, because immediately after that alloc and init new toolbar.

Even if the toolbar was valid when setting the frame, you cannot use its current y value as the new y value, because it will be 0 .

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

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

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]; 
+9
source
 CGRect rect2 = CGRectMake(0, toolBar.frame.origin.y , self.view.frame.size.width , 0); [toolbar setFrame:rect2]; 

try this i hope this can help you

0
source

Source: https://habr.com/ru/post/902552/


All Articles