设置UIView的layoutMargins不起作用

设置UIView的layoutMargins不起作用

本文介绍了设置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的布局边距



更新:


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:

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

08-28 01:43