ios 向 UIView 添加填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14856821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:16:44  来源:igfitidea点击:

Adding padding to an UIView

iosiphonecocoa-touchuiviewuikit

提问by Meda

I'm looking for a way to add a paddingproperty to an UIView. Ideally, I would like to avoid subclassing and putting it in a category. The usage would be something like:

我正在寻找一种padding向 UIView添加属性的方法。理想情况下,我想避免子类化并将其放在一个类别中。用法类似于:

myview.padding = UIEdgeInsetsMake(10, 10, 10, 10);

And maybe have a paddingBoxproperty as well which would return a CGRectdescribing the size and position of the inner padding box.

并且可能还有一个paddingBox属性,它会返回一个CGRect描述内部填充框的大小和位置的属性。

Now, how would one implement in a category something like that. I initially though of using bounds, but unfortunately the size of the boundsis linked to the size of the frame(always the same) only the coordinates can differ.

现在,如何在一个类别中实现类似的东西。我最初虽然使用bounds,但不幸的bounds是, 的大小与frame(始终相同)的大小相关联,只有坐标可以不同。

回答by George Green

This is generally done by setting the bounds within the view. So if you wanted an inset of 10 all round you could do:

这通常是通过在视图中设置边界来完成的。因此,如果您想要全方位插入 10,您可以这样做:

view.bounds = CGRectInset(view.frame, 10.0f, 10.0f);

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.

边界定义了视图的可绘制区域,相对于框架。所以这实际上应该提供一个填充。然后,您可以从边界获取“paddingBox”。

Hope this helps! :)

希望这可以帮助!:)

回答by Marcus Vinicius Kuquert

Update for Swift 3

Swift 3 更新

view.bounds = view.frame.insetBy(dx: 10.0, dy: 10.0)

:)

:)

回答by yageek

Since iOS 8, each view has now a layoutMarginsproperty which corresponds to the padding.

从 iOS 8 开始,每个视图现在都有一个layoutMargins与填充相对应的属性。

myview.layoutMargins = UIEdgeInsetsMake(10, 10, 10, 10);

When you use AutoLayout, a format with |-[subview]-|, |-will refer to the edges defined with layoutMargins.

当您使用自动版式,用格式|-[subview]-||-将引用与定义的边缘layoutMargins

Source: https://developer.apple.com/reference/uikit/uiview/1622566-layoutmargins?language=objc

来源:https: //developer.apple.com/reference/uikit/uiview/1622566-layoutmargins?language=objc

回答by Gandalf458

What you really have to do is create a view and add a subview to it. Make one view the background and give it the frame you want. Then make the second subview the frame you want with the edge insets.

您真正需要做的是创建一个视图并向其添加一个子视图。制作一个视图背景并为其添加所需的框架。然后使用边缘插入使第二个子视图成为您想要的框架。

UIView backgroundView = new UIView(CGRect.FromLTRB(0, 0, 100, 100))
{
    BackgroundColor = backgroundGray,
};

//Will have a right edge inset of 10
UIView edgyView = new UIView(CGRect.FromLTRB(0, 0, 90, 100))
{
    BackgroundColor = backgroundGray,
}

backgroundView.AddSubview(edgyView);

回答by Omar Albeik

You can override the alignmentRectInsets property. Here is an example in Swift 4

您可以覆盖alignmentRectInsets 属性。这是 Swift 4 中的一个例子

class YourCustomView: UIView {

    override var alignmentRectInsets: UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
}

回答by Boomerange

Update for Swift 4:

Swift 4 更新:

self.yourView.layoutMargins = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)

回答by Radu Ciobanu

You can inset the view's frame/bounds like this:

您可以像这样插入视图的框架/边界:

    yourView.frame = yourView.frame.inset(by: UIEdgeInsets(top: .zero, left: 5.0, bottom: 5.0, right: .zero)