【问题标题】:Adding padding to an UIView向 UIView 添加填充
【发布时间】:2013-01-29 04:36:35
【问题描述】:

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

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

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

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

【问题讨论】:

  • 如果你想到的是 Android 意义上的padding——在其中绘制视图背景而不是其内容的空间——我认为 iOS 只是没有将这个概念内置到它的视图中。很奇怪吧?

标签: ios iphone cocoa-touch uiview uikit


【解决方案1】:

这通常通过在视图中设置边界来完成。所以如果你想要一个 10 的插页,你可以这样做:

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

边界定义了视图的可绘制区域,相对于框架。所以这应该有效地填充。然后,您可以仅从边界获取“paddingBox”。

希望这会有所帮助! :)

在 Swift 5+ 中更新,它是

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

【讨论】:

  • 不幸的是,这也会改变视图的大小。正如我在问题中提到的那样,文档声明The size of the bounds rectangle is coupled to the size of the frame rectangle, so that changes to one affect the other.。换句话说,您发布的代码应该有 -10 而不是 10,即便如此,它只会添加顶部和左侧填充,但我会再次测试,以确保我没有遗漏任何东西。
  • 你有文档的链接吗,因为我很确定边界和框架的大小可以不同?
  • 嗯,刚刚在文档中找到,我只是在做一个实验来确认。我现在想道歉,因为事实确实如此。看起来你可能需要一个简单的子类。
  • self.contentTextView.bounds = self.contentTextView.frame.insetBy(dx: 10, dy: 10) (Swift 5+ )
【解决方案2】:

您真正需要做的是创建一个视图并向其添加一个子视图。将一个视图设置为背景并为其提供所需的框架。然后让第二个子视图成为你想要的带有边缘插图的框架。

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);

【讨论】:

    【解决方案3】:

    Swift 3 更新

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

    :)

    【讨论】:

      【解决方案4】:

      更新:从iOS11开始你应该使用directionalLayoutMargins而不是layoutMargins

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

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

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

      当您使用 AutoLayout 时,|-[subview]-||- 的格式将引用layoutMargins 定义的边缘。

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

          Swift 4 更新:

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

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-12-15
              • 2019-07-31
              • 1970-01-01
              • 1970-01-01
              • 2020-11-20
              • 2012-02-06
              相关资源
              最近更新 更多