UIToolbar - 边距?有谁知道 UIToolbar 的左右边距吗?
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为此信息不可用。
但通过一些测试应该很容易得到。
如果您需要的只是整个工具栏上的 UILabel,我建议将其添加到
UIToolbar
,它是UIView
子类。您不需要您的案例中的所有UIBarButtonItem
功能......只需要我认为的背景。然后设置
UILabel.frame
(使用您想要的边距)+addSubview:
+ aUITextAlignmentCenter
应该可以完成这项工作!另外也许还有一个 autoresizingMask 和UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
如果您有管理设备方向...编辑 1 :
由于对该提案存在一些疑问,我做了一个快速测试:
这是结果:
编辑 2 :
现在我已经启动了 Xcode 并编写了一些代码,很容易找出您的主要内容问题。稍微改变一下之前的代码:
带来:
正如你所猜测的,左边距为 12px,但是自定义视图看起来不像要调整大小以适应,因此,在这种情况下没有右边距......除非您相应地调整视图大小。
编辑3:
除非你的 UILabel 需要背景,否则我可能会这样做(这就是
UIBarButtonSystemItemFlexibleSpace
的用途...) :结果:
编辑 4 :
作为一方,在工作更多之后在
UIToolbar
上,12px
是在按钮之前添加的默认空间。我刚刚发现了一个有趣的现象,间隔符正在使用负值!。 IE 如果你想在两个按钮之间有 2px 的空间,只需添加一个 -10pxUIBarButtonSystemItemFixedSpace
... 方便!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 aUIView
subclass. You don't need all theUIBarButtonItem
features in your case.... just the background I presume.Then setting
UILabel.frame
(with the margins you want) +addSubview:
+ aUITextAlignmentCenter
should do the job! Plus maybe also a autoresizingMask withUIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
if you have manage device Orientation...EDIT 1 :
Since there are some doubts about that proposal, I made a quick test :
Here is the result :
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 :
brings that :
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...) :The result :
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 -10pxUIBarButtonSystemItemFixedSpace
... Handy!似乎没有任何地方存储此边距,但这里有一种在 Xamarin 中计算此边距的方法。首先向 ViewController 中的工具栏添加一个按钮:
然后使用此代码读取(使用断点)任何特定方向和设备的填充值。 (我已经包含了一个方向变量,因为如果您在旋转事件期间调用此代码,这将告诉您应用程序当前的方向是什么。)
根据记录,对于 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:
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.)
For the record, for iOS9, the values are 16 for portrait and 20 for landscape on iPhones, and 20 for both on iPads.