2

I have a UIViewController (using Swift)

It is just a simple list of Items. It is implemented as a UIViewController to which I have added a UITableView. This 'Items' view is invoked via this code in my slide menu implementation, and the initial view of the slide menu is embeded in a NavigationController - so all the views in the slide menu take on the navigation controller. Items is one of those slide menu views invoked like this

self.openViewControllerBasedOnIdentifier("Items")

This works fine and I get....

Initial Lists view from slider menu

I then have an add button in the top right that you can push to add new Items. The add button brings up a PageViewController. I use page view because there are multiple pages of details associated with the Item being added. This also works so far in that I get the pageviewcontroller launched and I can flip between pages.

The problem is that the placement of elements on the page shifts under some circumstances (it is not consistent).

When I initially show the 1st page of the PageViewController I see the layout that I designed (on the left below). If I simply tab out of the Text field OR if I swipe to the 2nd page and then swipe back to the 1st page then I see the right image. The whole view jumps up!

I tried flipping all the different settings in the storyboard regarding . I even moved the majority of the drawing in to the code and out of storyboard in order to manually set constraints. These are the settings I played with. tried lots of combinations but they are mostly all off now.

enter image description here

Here is Before and After. How do I stop the view from jumping up and down. Also, interesting is that if i swipe from page-3 to page-2 then page-2 is shifted up. If I keep going back to page-1 and then swipe back to page-2 then page-2 is back down to correct position. So deepening on which way I enter page-2 it is different layout. From 1 to 2, page 2 is correct. From 3 to 2, page 2 is shifted up.

enter image description here

I can not get the layout to stay in one spot.

The other option, is to just shut off NavigationController when in the page view controller and deal without having it.

So...

1) how can I stop the jumping - best case

2) how can I shut off navigation controller when pageviewcontroller is up - second option

The pages of my pageviewcontroller are invoked via this code

 instantiateViewController(withIdentifier: "itemDetail\(num)ViewController")

Where 'num' is 1,2, or 3. Using an indexed array and viewControllerBefore along with viewControllerAfter methods. The paging itself works fine.

Thanks

john
  • 505
  • 1
  • 7
  • 14

2 Answers2

0

This does not answer the question of how to make storyboard work. That remains a mystery. HOWEVER, I was able to fix the strange behavior of shifting layout by doing the following.

I removed ALL of the drawing from Storyboard and put everything in the code. For example, I have defined a container view ...

// The container
let containerView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.white
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.masksToBounds = true
    return view
}()

Then later I place the container relative to the UIViewController's view

    view.addSubview(containerView)

    containerView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
    containerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 155).isActive = true
    containerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 0).isActive = true
    containerView.heightAnchor.constraint(equalTo: view.heightAnchor, constant: -155).isActive = true

Similarly, I have defined all of the other controls in the code and then added them to the containerView subView and made then all relative to the containerView.

This keeps everything in place as I swipe between pages of the PageViewController

john
  • 505
  • 1
  • 7
  • 14
0

I had the same problem, and found the solution. You just need to disable automaticallyAdjustsScrollViewInsets in your viewDidLoad. Here's my code:

override func viewDidLoad()
{
    super.viewDidLoad()
    self.dataSource = self

    self.automaticallyAdjustsScrollViewInsets = false
    if let detailsVC = viewControllersList.first as? DetailsViewController
    {
        detailsVC.delegate = self
        self.setViewControllers([detailsVC], direction: .forward, animated: true, completion: nil)
    }
}

Credits to this answer.

Kasra Babaei
  • 260
  • 3
  • 9