Setting layoutMargins of UIView doesn't work

23,896

Solution 1

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.

Solution 2

This has been changed for iOS 11. Custom margins seems working very well when having deployment target setup for this iOS version. One day we will laugh at what we experienced before.

If you want to setup values lower than a system minimum (which may vary based on a device) - you have to also set viewRespectsSystemMinimumLayoutMargins for false (it's true by default).

If you are targeting iOS 10 and lower you are out of luck, @Reverend answer is correct - no layout margin customizations for viewcontroller's root view.

Share:
23,896
Marcin
Author by

Marcin

Passionate developer. iOS. https://github.com/krzyzanowskim

Updated on July 30, 2022

Comments

  • Marcin
    Marcin almost 2 years

    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