6

Since ios 8.0 views have additional layoutMargins which by default has an 8 points values for every side.

When I try to change margins in viewDidLoad it seems to have no effect on the child views:

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

}

..itd does not seem to have any effect and

aldorain
  • 790
  • 5
  • 16
  • Possible duplicate of [Setting layoutMargins of UIView doesn't work](http://stackoverflow.com/questions/27421469/setting-layoutmargins-of-uiview-doesnt-work) – klaaspieter Feb 10 '17 at 14:28

3 Answers3

5

You are trying to set the layout margin of a root view of a view controller. The root view behaves differently than any other view in the hierarchy in regard of the layout margins.

Layout margins of a root view are managed exclusively by the view controller, you can not set them.

From Apple documentation:

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.

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

Denis Krivitski
  • 654
  • 8
  • 11
4

I used KVO to detect the view.layoutMargins changes. And I found that the view itself would change the layoutMargins after customise during layoutSubViews processing.

So just put your customise codes in viewDidLayoutSubviews method:

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

PS: Through these codes are OC, but I think it can also work for Swift. Hope this works for you!

mayqiyue
  • 1,155
  • 1
  • 10
  • 16
  • 1. contrary to what everyone says in the answers found [here](https://stackoverflow.com/q/27421469/5175709) I was able to use this to change the layoutMargins for my ViewController's view. 2. I actually thought that there won't be any restrictions on changing a subview's layoutMargin upon its instantiation, but there actually seems that such restrictions are placed on subviews a well, because non of my modifications worked. The only place where changing a subviews margins worked was in `viewDidLayoutSubviews` – mfaani Dec 03 '17 at 00:41
0

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

This prints 100 for every margin. If you are trying to set the margins for subviews of that view, you will need to set them explicitly also. The call to self.view.layoutMargins only affects the layoutMargins of self.view.

If you want your view's subviews to respect the layoutMargins of your view's superview, you will need to set preservesSuperviewLayoutMargins on your view.

Reference Documentation

Ideasthete
  • 1,553
  • 13
  • 22
  • Should have added additional info - when I `println` layout values i get the default ones not the 100'a – aldorain Sep 12 '14 at 06:23
  • @aldorain Are you sure you are changing the layoutMargins of the right view, then? Unless there is something going on behind the scenes that you aren't showing, there is no reason you should be getting different results for the exact same code. – Ideasthete Sep 12 '14 at 15:50
  • 3
    They get changed back sometime between `viewWillAppear` and `viewDidAppear`--and then will continue to be changed for reasons currently unknown. I've yet to find the right hook to use in order to preserve custom margins. – Christopher Swasey Dec 28 '14 at 17:34
  • I used KVO to detect the view.layoutMargins changes. And I found that the view itself would change the layoutMargins after customise. – mayqiyue Jan 09 '16 at 06:30