简体   繁体   English

如何获取导航栏中栏按钮项的布局边距?

[英]How to get layout margins for bar button item in navigation bar?

I'm trying to create section headers in a collection view that look similar to section headers in a table view.我正在尝试在集合视图中创建类似于表视图中的节标题的节标题。

The text in a table view section header is aligned with the left bar button item.表格视图部分 header 中的文本与左侧栏按钮项对齐。 How do I get this alignment layout value in a collection view?如何在集合视图中获取此 alignment 布局值?

In a table view, this alignment value is in the layout margins property.在表格视图中,此 alignment 值位于布局边距属性中。 I'm not able to find something equivalent in a collection view.我无法在集合视图中找到等效的东西。

tableView.layoutMargins.left

在此处输入图像描述

A UIBarButtonItem is not a descendent of UIView. UIBarButtonItem 不是 UIView 的后代。 That's the problem.那就是问题所在。 Whatever Apple is doing under the covers with the UINavigationBar is not publicly accessible.无论 Apple 使用 UINavigationBar 在幕后做什么,都不能公开访问。 I think your best bet is a kind of hack.我认为你最好的选择是一种黑客行为。

Somewhere in your initialization code, do the following:在初始化代码中的某处,执行以下操作:

UITableView* temp = [[UITableView alloc] init];
UIEdgeInsets insets = temp.layoutMargins;
NSLog(@"%f", insets.left);

Then, save the UIEdgeInsets object and let the table disappear into the ether.然后,保存 UIEdgeInsets object 并让表格消失在以太中。

The NSLog in the above example prints out 8.000000.上面示例中的 NSLog 打印出 8.000000。 It seems like Apple is just using a standard value.似乎Apple只是使用标准值。 Alternately, you could use that.或者,您可以使用它。 But if you want to avoid hard-coding a standard value, then do what I suggest.但是,如果您想避免硬编码标准值,请执行我的建议。

I'll admit, you're relying on the table, not the left bar button.我承认,您依赖的是表格,而不是左侧的栏按钮。 But, I'm guessing what you want is to do whatever table views are doing.但是,我猜你想要的是做任何表格视图正在做的事情。 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM