Kodeco Forums

Video Tutorial: Swift Scroll View School Part 13: Slide-out Sidebar I

In this video tutorial you'll learn how to build a slide-out sidebar navigation menu using scroll views.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3911-scroll-view-school/lessons/14

Thank you for this video, how to make the sidebar is accessible from all pages. thank you in advance for your advice
Fabrice

Hi,

Would you already have the version working on Swift 3.0? I have errors when I write the NS Layout constraints, options can’t be nil anymore, and when I use the symbol | it says that this doesn’t work either so I don’t know how I can fix this part.

Thanks,
Seb

The challenge solution won’t work if you change device orientation. You can do this instead:

var menuVisible = false

override func viewDidLayoutSubviews() {
    if !menuVisible {
        closeMenuAnimated(false)
    }
}

Then in closeMenuAnimated(_: )

 func closeMenuAnimated(_ animated: Bool) {
    switch UIDevice.current.orientation {
    case .portrait, .portraitUpsideDown:
      scrollView.setContentOffset(CGPoint(x: leftViewController.view.frame.width, y: 0), animated: animated)
    default:
      scrollView.setContentOffset(CGPoint(x: leftViewController.view.frame.height, y: 0), animated: animated)
    }
    
    menuVisible = false
  }

You can then conform SidebarViewController to UIScrollViewDelegate:

// MARK: - UIScrollViewDelegate
extension SidebarViewController: UIScrollViewDelegate {
  func scrollViewDidScroll(_ scrollView: UIScrollView) {
    menuVisible = (scrollView.contentOffset == .zero)
  }
}

Hi,

is using a flag field and viewDidLayoutSubviews() the best practice for doing initial stuff after auto layout?

Thanks,
Icey