无法更改默认布局

ald*_*ain 4 objective-c ios swift

由于ios 8.0视图具有其他功能layoutMargins,因此默认情况下,每侧具有8点的值。

当我尝试更改边距时,viewDidLoad它似乎对子视图没有影响:

override func viewDidLoad(){
    super.viewDidLoad()
    self.view.layoutMargins = UIEdgeInsets(top:100, left:100, bottom:100, right:100)

}
Run Code Online (Sandbox Code Playgroud)

..itd似乎没有任何作用,并且

Den*_*ski 5

您正在尝试设置视图控制器的根视图的布局边距。在布局边距方面,根视图的行为与层次结构中的任何其他视图不同。

根视图的布局边距仅由视图控制器管理,您无法设置它们。

从Apple文档中:

如果该视图是视图控制器的根视图,则系统将设置并管理页边距。上下边距设置为零点。边距取决于当前的尺寸级别,但可以为16或20点。您无法更改这些边距。

https://developer.apple.com/reference/uikit/uiview/1622566-layoutmargins


Ide*_*ete 0

我不确定问题是什么。

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.layoutMargins = UIEdgeInsets(top:100, left:100, bottom:100, right:100)
    println(self.view.layoutMargins.top)
    println(self.view.layoutMargins.left)
    println(self.view.layoutMargins.right)
    println(self.view.layoutMargins.bottom)
}
Run Code Online (Sandbox Code Playgroud)

这将为每个边距打印 100。如果您尝试设置该视图的子视图的边距,则还需要显式设置它们。对 self.view.layoutMargins 的调用仅影响 self.view 的layoutMargins。

如果您希望视图的子视图尊重视图的超级视图的layoutMargins,则需要在视图上设置preservesSuperviewLayoutMargins。

参考文档

  • 它们会在“viewWillAppear”和“viewDidAppear”之间的某个时间被更改回来——然后由于目前未知的原因将继续更改。我还没有找到合适的挂钩来保留自定义边距。 (3认同)