Dennish
Dennish

Reputation: 116

Trying to understand layout constraints for iOS 9.3 support programmatically

I have a tabViewController with a navigatonController as tab 1. The root view of the navigationController is listView and selecting an item pushes a pageViewController onto the stack. The pageVC manages an array of the same viewController which has an scrollView with dynamic content and scrolls vertically. The scrollView is pinned to the anchors of the viewController with this code:

    if #available(iOS 11.0, *) {
        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0),
            scrollView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0),
            scrollView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0),
            scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0)
            ])
    }else {
        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
            scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
            scrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
            ])
    }

I need to support iPhone 4s (iOS 9.3) devices.

The dynamic content in the scrollView and scrolling is all working as expected on iOS 11 devices with the content starting below the nav bar and scrolling stops at the top of the tab bar.

The problem I have is on iOS versions less than 11. The scrollView content is initially displayed correctly but if I tap or scroll the content, the top moves behind the nav bar and I cannot see the top of the scrollView anymore. The same for the bottom of the scrollView and the tab bar.

I have tried several stack suggestions for edgesForExtendedLayout, scrollView insets etc but can't find the right solution. I would ideally like to understand constraints, layout guides etc programmatically and any links to tutorials would be great for another day.

Upvotes: 1

Views: 165

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100549

Can you try this in else

  let navBarView = self.navigationController.navigationBar

  let tabBarView = self.tabBarController.tabBar

  NSLayoutConstraint.activate([
        scrollView.topAnchor.constraint(equalTo: navBarView.bottomAnchor, constant: 0),
        scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
        scrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0),
        scrollView.bottomAnchor.constraint(equalTo: tabBarView.topAnchor, constant: 0)
        ])

Upvotes: 0

Related Questions