Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing XY position of UI Elements / Objects

I want to know why you are unable to change the XY position of some objects in objective-c? For example... I had a toolbar with the following code, but it ONLY works if I place it WITHIN a UIView and then set the same coordinates... Can anyone explain why?

Doesn't Work:

 UIToolbar *commentstoolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 400, 320, 44)];
 [self.view addSubview:commentstoolbar];

Does Work:

 UIToolbar *commentstoolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
 UIView *toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 400, 320, 44)];

 [toolbarView addSubview:commentstoolbar];
 [self.view addSubview:toolbarView];
like image 674
rckehoe Avatar asked Apr 16 '26 03:04

rckehoe


1 Answers

The UIToolbar conforms to the UIBarPositioning protocol, which specifies:

The UIBarPositioning protocol defines the ways that bars can be positioned on iOS devices. Bars can be positioned at the bottom of their enclosing view, at the top of their enclosing view, or at both the top of their enclosing view and also the top of the screen.

So the way to solve place a Tool bar anywhere is to enclose it in a view, just as you are doing.

like image 81
rr1g0 Avatar answered Apr 18 '26 15:04

rr1g0