Explain this field logic for leftBarButtonItem, rightBarButtonItem, titleView

Can someone explain to me where the fields you see in this screenshot come from? I want all the red, green and blue rectangles to match each other both on the screen layouts, and in the landscape and in the portrait. Instead, I see inexplicable fields between views.

// Setup Left Bar Button item
UIBlankToolbar* tools = [[[UIBlankToolbar alloc] initWithFrame:CGRectMake(0, 0, 115, 44)] autorelease];
tools.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[tools setBackgroundColor:[UIColor greenColor]];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:tools] autorelease];

...

// Setup Right Bar Button Item
UIBlankToolbar* tools = [[[UIBlankToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 44)] autorelease];
[tools setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[tools setBackgroundColor:[UIColor redColor]];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:tools] autorelease];

...

// Setup Title View
self.navigationItem.titleView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 44)] autorelease];
self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.navigationItem.titleView setBackgroundColor:[UIColor blueColor]];

What I see with this code:

enter image description hereenter image description here

What really puzzles me is that the margins between the views are getting BIG as the available space is getting smaller? I don’t understand why they are there, and why they behave against what I expect from the fields.

Thank!

+5
1

, , .

: 115+100+50 = 265, - 320, 55 . , , . , , , .

, , . , undefined, .. , , .

, . 100 100+(480-320) = 260. , , . , , , , , .

. , , undefined, , , , .

, undefined, undefined . , . , .

yourView = [[YourView alloc] initWithFrame:(UIInterfaceOrientationIsPortrait(interfaceOrientation)) ? CGRectMake(0, 0, 320, 44) : CGRectMake(0, 0, 480, 44)];
yourView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.navigationController.navigationBar addSubview:yourView];
+4

All Articles