Unable to change the default format.

Since ios 8.0 views have an optional layoutMargins , which by default has 8 points for each side.

When I try to change the fields in viewDidLoad , this does not seem to affect child views:

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

.. itd has no effect and

+4
ios objective-c swift
Sep 11 '14 at 10:47
source share
3 answers

I used KVO to detect changes to view.layoutMargins. And I found that the view itself will change layoutMargins after setting during processing of layoutSubViews .

So, just enter your settings codes into the viewDidLayoutSubviews method:

 - (void)viewDidLayoutSubviews { self.view.layoutMargins = UIEdgeInsetsMake(100.0f, 100.0f, 100.0f, 100.0f); } 

PS: There is an OC through these codes, but I think it can also work for Swift. Hope this works for you!

+4
Jan 09 '16 at 6:39
source share

You are trying to set the layout layout of the root view controller of the view. The root view behaves differently than any other view in the hierarchy with respect to layout fields.

The layout fields of the root view are controlled exclusively by the view controller; you cannot set them.

From the Apple documentation:

If the view is the root view of the view controllers, the system sets up and manages the fields. The upper and lower fields are set to zero points. Side margins vary depending on the current size class, but can be either 16 or 20 points. You cannot change these fields.

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

+2
Mar 28 '17 at 7:57
source share

I am not sure what the problem is.

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

Fingerprint 100 for each field. If you are trying to set fields for subviews of this view, you will also need to explicitly specify them. Calling self.view.layoutMargins only affects layoutMargins self.view.

If you want your views to take into account the layout layouts of your view, you will need to set saveesSuperviewLayoutMargins in your view.

Reference documentation

0
Sep 11 '14 at 16:53
source share



All Articles