设置 UIView 的 layoutMargins 不起作用 [英] Setting layoutMargins of UIView doesn't work

查看:35
本文介绍了设置 UIView 的 layoutMargins 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIViewControllerUIViewUITableView

UIView
|-UITableView

我正在尝试设置这样的边距:

I'm trying to setup margins like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.layoutMargins = UIEdgeInsetsMake(30, 30, 30, 30);
    self.tableView.preservesSuperviewLayoutMargins = YES;
    [self.view layoutIfNeeded];
}

但是视图上什么也没有发生.

but nothing is happening on the view.

这里是 InterfaceBuilder 的约束

Here are the constraints from InterfaceBuilder

(lldb) po self.view.constraints
<__NSArrayM 0x786ab6e0>(
<NSLayoutConstraint:0x7896e940 UIView:0x7896e470.trailingMargin == UITableView:0x79b51a00.trailing - 16>,
<NSLayoutConstraint:0x7896e970 UITableView:0x79b51a00.leading == UIView:0x7896e470.leadingMargin - 16>,
<NSLayoutConstraint:0x7896e9a0 V:[_UILayoutGuide:0x7896e510]-(0)-[UITableView:0x79b51a00]>,
<NSLayoutConstraint:0x7896e9d0 V:[UITableView:0x79b51a00]-(0)-[_UILayoutGuide:0x7896e600]>,
<_UILayoutSupportConstraint:0x7896c7d0 V:[_UILayoutGuide:0x7896e510(0)]>,
<_UILayoutSupportConstraint:0x7896c2b0 V:|-(0)-[_UILayoutGuide:0x7896e510]   (Names: '|':UIView:0x7896e470 )>,
<_UILayoutSupportConstraint:0x7896cbf0 V:[_UILayoutGuide:0x7896e600(0)]>,
<_UILayoutSupportConstraint:0x7896ea00 _UILayoutGuide:0x7896e600.bottom == UIView:0x7896e470.bottom>
)

结果看不到任何边距,根本没有任何变化......我在做什么?

as a result don't see any margins, nothing is changed at all.... What I'm issing ?

iOS 8

推荐答案

自定义 layoutMargins 不适用于作为 UIViewController 实例根的视图.这些是由系统定义的,不能被覆盖.您需要添加另一个子视图来保存您的所有内容,然后您可以在这个新的contentView"上修改布局边距

Custom layoutMargins don't work on a view that is the root to a UIViewController instance. Those are defined by the system and cannot be overridden. You need to add another subview that will hold all of your content, you can then modify the layout margins on this new "contentView"

更新:

讨论

使用此属性指定视图边缘和任何子视图之间所需的空间量(以点为单位).自动布局使用您的边距作为放置内容的提示.为了例如,如果您使用格式字符串|-[subview]-|",子视图的左右边缘由相应的布局从父视图的边缘插入边距.当您的视图边缘靠近视图的边缘时superview 和preservesSuperviewLayoutMargins 属性是YES,实际布局边距可能会增加以防止内容重叠父视图的边距.

Use this property to specify the desired amount of space (measured in points) between the edge of the view and any subviews. Auto layout uses your margins as a cue for placing content. For example, if you specify a set of horizontal constraints using the format string "|-[subview]-|", the left and right edges of the subview are inset from the edge of the superview by the corresponding layout margins. When the edge of your view is close to the edge of the superview and the preservesSuperviewLayoutMargins property is YES, the actual layout margins may be increased to prevent content from overlapping the superview’s margins.

默认的边距是每边八点.

The default margins are eight points on each side.

如果视图是视图控制器的根视图,则系统设置并管理利润.顶部和底部边距设置为零点.边距因当前尺寸等级而异,但可以是 16 或 20 点.您无法更改这些边距.

If the view is a view controller’s root view, the system sets and manages the margins. The top and bottom margins are set to zero points. The side margins vary depending on the current size class, but can be either 16 or 20 points. You cannot change these margins.

这篇关于设置 UIView 的 layoutMargins 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆