为leftBarButtonItem,rightBarButtonItem,titleView解释此边距逻辑 [英] Explain this margin logic for leftBarButtonItem, rightBarButtonItem, titleView

查看:367
本文介绍了为leftBarButtonItem,rightBarButtonItem,titleView解释此边距逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以向我解释一下您在以下屏幕快照中看到的边距来自何处吗?我希望红色,绿色和蓝色矩形在屏幕布局(横向和纵向)中都彼此相邻.相反,我看到了视图之间的莫名其妙的空白.

Can anyone explain to me where the margins you see in this screen shot below are coming from? I want the red, green and blue rectangles to all fit next to each other in both screen layouts, landscape and portrait. Instead I see inexplicable margins between the 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 really perplexes me is that the margins between views become BIGGER as the space available becomes smaller? I don't understand why they are there, nor why they behave counter to what I expect from margins.

谢谢!

推荐答案

导航栏在其强制执行的项目之间具有最小的边距,这就是为什么您不能摆脱边距的原因.

The navigation bar has minimum margins between its items that it enforces and that's why you can't get rid of the margins.

解释您的特定代码也不是很困难:115+100+50 = 265并且屏幕的宽度为320,因此导航栏有55个像素用作边距.左侧项目左对齐,右侧项目右对齐,中间项目居中. 因此,肖像中的边距非常有意义.

Explaining your specific code is not very hard either: 115+100+50 = 265 and the width of the screen is 320, so the navigation bar has 55 pixels to use as margins. The left item is left aligned, the right item is right aligned and the middle item is centered. So the margins you get in portrait makes perfect sense.

您在风景中看到的是由于您的宽度灵活.您不应在导航栏中的项目上设置灵活的宽度,因此执行此操作时的行为是不确定的,即,错误在于您如何使用导航栏,而不是在导航栏本身中.

What you're seeing in landscape is due to your flexible width. You're not supposed to have flexible width on items in a navigation bar, so the behavior when doing that is undefined, i.e., the bug is in how you use the navigation bar and not in the navigation bar itself.

正在发生的事情是,首先计算正确项目的新尺寸.它从100增长到100+(480-320) = 260.然后计算中间项目的大小,看起来它只是占用了所有可用的大小.是时候计算左项了,实际上是缩小了,因为右项和中间项已经占据了所有空间,因此,当导航栏强制执行边距时,像素就是从左项中获取的.

What's happening is that the new size for the right item is calculated first. It grows from 100 to 100+(480-320) = 260. Then the middle item's size is calculated and it seems like it just takes all the size available to it. When it's time for the left item to be calculated it actually shrinks since the right and middle item has taken all the space, so when the navigation bar enforces the margins the pixels are taken from the left item.

实际算法可能有所不同.毕竟它是封闭的,我试图解释未定义的行为,但是查看所产生的利润似乎正在发生.

The actual algorithm may differ. After all it's closed source and I'm trying to explain undefined behavior, but looking at the produced margins that seems to be what's happening.

无论如何,您的应用程序永远不要依赖未定义的行为,因为未定义的行为可能会在版本之间发生变化并破坏它.您应该做的是创建自己的自定义视图,并将其添加为导航栏的子视图.然后,您将按照自己的方式处理视图中的边距.

Anyway, you're app should never rely on undefined behavior, as undefined behavior may change between versions and break it. What you should do is create your own custom view and add it as a subview of the navigation bar. Then you'll handle margins within your view the way you want to.

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

这篇关于为leftBarButtonItem,rightBarButtonItem,titleView解释此边距逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆