Halfpint
Halfpint

Reputation: 4079

UIView add padding from top

I am adding custom views to a UIPageViewController which is embedded inside of a UINavigationController, because of this I need to add a padding of 20.0 from the top of each child view inside of the UIPageViewController container in order for them to sit neatly under the navigation bar.

I'm currently using UIView's layoutMargins property, setting the new edge insets on the view before it is loaded into memory, here is my helper method for creating a new view:

private func instanciateViewController(pageType: ViewType) -> UIViewController? {

    var pageView : UIViewController?

    switch(pageType)
    {
        case .SHOP :
            pageView = storyboard!.instantiateViewControllerWithIdentifier("ShopFloorViewController")
            break
        case .TASKS :
            pageView = storyboard!.instantiateViewControllerWithIdentifier("TaskListTableViewController")
            break
        case .PERSONNEL :
            pageView = storyboard!.instantiateViewControllerWithIdentifier("PersonnelListViewController")
            break
    }
    pageView!.view.layoutMargins = UIEdgeInsetsMake(20, 0, 0, 0)
    return pageView
}

The line: pageView!.view.layoutMargins = UIEdgeInsetsMake(50, 0, 0, 0) provides the correct inset for the first screen loaded, however any interaction on the scene then causes the view to re-render with no insets.

How can I make the insets on the child views inside my UIPageViewController permanent?

Upvotes: 1

Views: 3936

Answers (1)

Eppilo
Eppilo

Reputation: 773

Try this (I haven't tested for this specific case but makes sense). Inside the UIPageViewController override viewWillLayoutSubviews. The default implementation of this method does nothing so just write your own code inside. Inside the method, loop through the subviews and adjust the edge insets. Hopefully this should work.

Upvotes: 2

Related Questions