Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting layoutMargins of UIView doesn't work

Tags:

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

like image 954
Marcin Avatar asked Dec 11 '14 11:12

Marcin


People also ask

What is layout margins iOS?

Layout margins provide a visual buffer between a view's content and any content outside of the view's bounds. The layout margins consist of inset values for each edge (top, bottom, leading, and trailing) of the view.

What is preserve superview margins?

Preventing Content in the Superview Margin This margin affects layouts where the distance between the edge of a view and its superview is smaller than the corresponding margin. If we set it on the red container view it increases the effective margin of the red view to match the superview margin.

What is constraint to margin in Xcode?

The “Constrain to margins” checkbox determines whether constraints to the superview use the superview's margins or its edges. The lower portion of the popover lets you set the item's width or height. The Width and Height constraints default to the current canvas size, though you can type in different values.


2 Answers

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.

like image 86
the Reverend Avatar answered Oct 10 '22 06:10

the Reverend


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.

like image 36
Viktor Kucera Avatar answered Oct 10 '22 08:10

Viktor Kucera