UIToolbar - 边距?有谁知道 UIToolbar 的左右边距吗?

发布于 2024-11-01 19:45:21 字数 334 浏览 4 评论 0原文

我正在创建一个 UIToolbar,上面只放置了一个 UILabel。标签内容是动态的并且可以更改,我想将其集中在 UIToolbar 上。

有谁知道 UIToolbar 左侧和右侧的确切边距大小?

我正在创建一个 [[UIBarButtonItem alloc] initWithCustomView:myLabelContainer] 并且我试图确保 myLabelContainer 占用整个工具栏空间,减去强制边距。

现在我猜测每边的边距约为 12 px,因此框架宽度为 296 的 UIView 应该填充整个 UIToolbar?

这些信息可以在任何地方获得吗?

I'm creating a UIToolbar with nothing but a UILabel placed on it. the label content is dynamic and can change and I'd like to center it on a UIToolbar.

Does anyone know the exact margin size for the left and right sides of a UIToolbar?

I'm creating a [[UIBarButtonItem alloc] initWithCustomView:myLabelContainer] and I'm trying to ensure that myLabelContainer takes up the entire toolbar space, minus the forced margins.

Right now I've guessed that the margins are about 12 px on each side so a UIView with a frame width of 296 should fill the entire UIToolbar?

Is this information available anywhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没︽人懂的悲伤 2024-11-08 19:45:21

我认为此信息不可用。
但通过一些测试应该很容易得到。

如果您需要的只是整个工具栏上的 UILabel,我建议将其添加到 UIToolbar,它是 UIView 子类。您不需要您的案例中的所有 UIBarButtonItem 功能......只需要我认为的背景。

然后设置 UILabel.frame (使用您想要的边距)+ addSubview: + a UITextAlignmentCenter 应该可以完成这项工作!另外也许还有一个 autoresizingMask 和 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight 如果您有管理设备方向...

编辑 1

由于对该提案存在一些疑问,我做了一个快速测试:

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 44)] autorelease];
label.text = @"Hello world";
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor blackColor];
label.backgroundColor = RGBACOLOR(255, 0, 0, .3f);
[self.toolbar addSubview:label];

这是结果:

在此处输入图像描述

编辑 2

现在我已经启动了 Xcode 并编写了一些代码,很容易找出您的主要内容问题。稍微改变一下之前的代码:

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
... same
[self.toolbar setItems:
 [NSArray arrayWithObject:
  [[[UIBarButtonItem alloc] initWithCustomView:label] autorelease]]];

带来:

toolbar2

正如你所猜测的,左边距为 12px,但是自定义视图看起来不像要调整大小以适应,因此,在这种情况下没有右边距......除非您相应地调整视图大小。

编辑3

除非你的 UILabel 需要背景,否则我可能会这样做(这就是 UIBarButtonSystemItemFlexibleSpace 的用途...) :

UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = @"Hello world";
... same
[label sizeToFit];
[self.toolbar setItems:
 [NSArray arrayWithObjects:
  [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                 target:nil action:nil] autorelease],
  [[[UIBarButtonItem alloc] initWithCustomView:label] autorelease],
  [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                 target:nil action:nil] autorelease],
  nil]];

结果:

在此处输入图像描述

编辑 4

作为一方,在工作更多之后在 UIToolbar 上,12px 是在按钮之前添加的默认空间。我刚刚发现了一个有趣的现象,间隔符正在使用负值!。 IE 如果你想在两个按钮之间有 2px 的空间,只需添加一个 -10px UIBarButtonSystemItemFixedSpace ... 方便!

I don't think this information is available.
But it should be pretty easy to get with some testing.

If what you need is just a UILabel over the whole toolbar, I would suggest just to add it to the UIToolbar, which is a UIView subclass. You don't need all the UIBarButtonItem features in your case.... just the background I presume.

Then setting UILabel.frame (with the margins you want) + addSubview: + a UITextAlignmentCenter should do the job! Plus maybe also a autoresizingMask with UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight if you have manage device Orientation...

EDIT 1 :

Since there are some doubts about that proposal, I made a quick test :

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 44)] autorelease];
label.text = @"Hello world";
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor blackColor];
label.backgroundColor = RGBACOLOR(255, 0, 0, .3f);
[self.toolbar addSubview:label];

Here is the result :

enter image description here

EDIT 2 :

Now that I've launced Xcode and wrote some code, that's easy to figure your primary question. Altering a bit the previous code :

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
... same
[self.toolbar setItems:
 [NSArray arrayWithObject:
  [[[UIBarButtonItem alloc] initWithCustomView:label] autorelease]]];

brings that :

toolbar2

So as you guessed, there is a left margin of 12px, but Custom views doesn't look like to be resized to fit, therefore, no right margin in that case... unless you resize your view accordingly.

EDIT 3 :

Unless your UILabel needs a background, here is what I would probably do (That's what UIBarButtonSystemItemFlexibleSpace are for after all...) :

UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = @"Hello world";
... same
[label sizeToFit];
[self.toolbar setItems:
 [NSArray arrayWithObjects:
  [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                 target:nil action:nil] autorelease],
  [[[UIBarButtonItem alloc] initWithCustomView:label] autorelease],
  [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                 target:nil action:nil] autorelease],
  nil]];

The result :

enter image description here

EDIT 4 :

As a side not, after working more on a UIToolbar, 12px is the default space added before a button. And I've just discovered a fun one, spacers are working with negative values!. I.E. if you want a 2px space between two buttons, just add a -10px UIBarButtonSystemItemFixedSpace... Handy!

雨落□心尘 2024-11-08 19:45:21

似乎没有任何地方存储此边距,但这里有一种在 Xamarin 中计算此边距的方法。首先向 ViewController 中的工具栏添加一个按钮:

UIButton button = new UIButton();
button.Tag = 999;
ToolbarItems = new[] { FilterButton };

然后使用此代码读取(使用断点)任何特定方向和设备的填充值。 (我已经包含了一个方向变量,因为如果您在旋转事件期间调用此代码,这将告诉您应用程序当前的方向是什么。)

if (NavigationController != null)
{
    UIButton firstButton = NavigationController.Toolbar.Subviews
        .FirstOrDefault(x => x.GetType() == typeof(UIButton) && x.Tag == 999);
    if (firstButton != null)
    {
        var orientation = UIApplication.SharedApplication.StatusBarOrientation;
        var padding = firstButton.Frame.X;
    }
}

根据记录,对于 iOS9,纵向值为 16, iPhone 上横向为 20,iPad 上横向均为 20。

There doesn't appear to be anywhere this margin is stored, but here's a way of calculating this in Xamarin. First add a button to the Toolbar in your ViewController:

UIButton button = new UIButton();
button.Tag = 999;
ToolbarItems = new[] { FilterButton };

Then use this code to read out (using a breakpoint) the value of the padding for any particular orientation and device. (I've included a variable for the orientation as this will tell you what the orientation of the app currently is if you're calling this code during a rotation event.)

if (NavigationController != null)
{
    UIButton firstButton = NavigationController.Toolbar.Subviews
        .FirstOrDefault(x => x.GetType() == typeof(UIButton) && x.Tag == 999);
    if (firstButton != null)
    {
        var orientation = UIApplication.SharedApplication.StatusBarOrientation;
        var padding = firstButton.Frame.X;
    }
}

For the record, for iOS9, the values are 16 for portrait and 20 for landscape on iPhones, and 20 for both on iPads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文