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

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

问题描述

UIViewController ,带 UIView UITableView

  UIView 
| -UITableView

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

   - (void)viewDidLoad {
[super viewDidLoad];

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

但视图上没有发生任何事情。



以下是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](姓名:'|':UIView:0x7896e470)>,
< _UILayoutSupportConstraint:0x7896cbf0 V:[_ UILayoutGuide:0x7896e600(0)]> ;,
< _UILayoutSupportConstraint:0x7896ea00 _UILayoutGuide:0x7896e600.bottom == UIView:0x7896e470.bott OM>

因此看不到任何保证金,没有任何改变。 ...我正在发布什么?



iOS 8

解决方案

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



更新:


讨论



使用此属性指定所需视图边缘与任何子视图之间的空间量(以点为单位)。
自动布局使用您的边距作为放置内容的提示。对于
示例,如果使用
格式字符串| - [subview] - |指定一组水平约束,子视图
的左右边缘将从边缘插入通过相应布局
边距的超视图。当您的视图边缘接近
超级视图的边缘并且preservesSuperviewLayoutMargins属性为YES时,可以增加
实际布局边距以防止
中的内容与superview的边距重叠。 / p>

默认边距是每边八个点。



如果视图是视图控制器的根视图,系统集和
管理边距。顶部和底部边距设置为零
点。边距取决于当前尺寸等级,但
可以是16或20点。您无法更改这些边距。



UIViewController with UIView and UITableView

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.

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

解决方案

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"

Update:

Discussion

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.

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天全站免登陆