添加填充到UIView [英] Adding padding to an UIView
问题描述
我正在寻找一种方法来向UIView添加 padding
属性。理想情况下,我想避免子类化并将其放入类别中。用法如下:
I'm looking for a way to add a padding
property to an UIView. Ideally, I would like to avoid subclassing and putting it in a category. The usage would be something like:
myview.padding = UIEdgeInsetsMake(10, 10, 10, 10);
并且可能还有 paddingBox
属性这将返回 CGRect
,描述内部填充框的大小和位置。
And maybe have a paddingBox
property as well which would return a CGRect
describing the size and position of the inner padding box.
现在,如何实现在类似的东西。我最初使用 bounds
,但不幸的是 bounds
的大小与<$ c的大小相关联$ c> frame (总是一样的)只有坐标可以不同。
Now, how would one implement in a category something like that. I initially though of using bounds
, but unfortunately the size of the bounds
is linked to the size of the frame
(always the same) only the coordinates can differ.
推荐答案
这一般是通过在视图中设置边界来完成。因此,如果你想要一个10的插图你可以做:
This is generally done by setting the bounds within the view. So if you wanted an inset of 10 all round you could do:
view.bounds = CGRectInset(view.frame, 10.0f, 10.0f);
边界定义视图相对于框架的可绘制区域。所以这应该实际上是一个填充。然后你可以从边界获得'paddingBox'。
The bounds defines the drawable area of the view, relative to the frame. So this should give in effect a padding. You can then get the 'paddingBox' just from the bounds.
希望这会有所帮助! :)
Hope this helps! :)
这篇关于添加填充到UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!