esi*_*ver 5 iphone layout uibarbuttonitem uibarbuttonitemstyle
任何人都可以向我解释你在下面这个屏幕截图中看到的边距来自哪里?我希望红色,绿色和蓝色矩形在屏幕布局,横向和纵向两者中彼此相邻.相反,我在视图之间看到了莫名其妙的边缘.
// 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]];
Run Code Online (Sandbox Code Playgroud)
我在这段代码中看到的内容:

让我感到困惑的是,随着可用空间变小,视图之间的边距变得更大?我不明白为什么他们在那里,也不明白为什么他们的行为与我对利润的期望相反.
谢谢!
导航栏在其强制执行的项目之间具有最小边距,这就是您无法消除边距的原因。
解释你的具体代码也不是很难:115+100+50 = 265屏幕的宽度是 320,所以导航栏有 55 像素用作边距。左侧项目左对齐,右侧项目右对齐,中间项目居中。所以你在肖像中获得的边距是非常有意义的。
您在横向中看到的内容是由于您灵活的宽度所致。您不应该在导航栏中的项目上具有灵活的宽度,因此这样做时的行为是未定义的,即错误在于您如何使用导航栏而不是导航栏本身。
发生的情况是,首先计算正确项目的新尺寸。它从 100 增长到100+(480-320) = 260. 然后计算中间项目的尺寸,看起来它只需要所有可用的尺寸。当计算左侧项目时,它实际上会缩小,因为右侧和中间项目占据了所有空间,因此当导航栏强制执行边距时,像素将从左侧项目中获取。
实际算法可能有所不同。毕竟它是闭源的,我试图解释未定义的行为,但看看产生的利润似乎就是正在发生的事情。
无论如何,您的应用程序永远不应该依赖于未定义的行为,因为未定义的行为可能会在版本之间发生变化并破坏它。您应该做的是创建自己的自定义视图并将其添加为导航栏的子视图。然后您将按照您想要的方式处理视图中的边距。
yourView = [[YourView alloc] initWithFrame:(UIInterfaceOrientationIsPortrait(interfaceOrientation)) ? CGRectMake(0, 0, 320, 44) : CGRectMake(0, 0, 480, 44)];
yourView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.navigationController.navigationBar addSubview:yourView];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5244 次 |
| 最近记录: |