Code Monkey home page Code Monkey logo

ios-top-down-splitview's Introduction

Summary

Wherein I create a simple top-bottom split view, incorporating pull-to-reveal.

Intro

I needed a container view similar to a split view, but more like a pull-down to refresh. I looked at various options including MGSplitViewController.

There are 3 reasons I did not even attempt to use this library:

  1. The open issues hinted at iOS 8 issues.
  2. The project seems quite inactive
  3. My use-case is sufficiently narrow compared to the complexity this library offers.

So I decided to implement this myself, and it was suprisingly simple. Here are the requirements:

  1. Upon loading the screen should show a primary view that takes up the entire screen (on iPhone devices)
  2. A seconary view abutting the primary view's top border should be revealed when a user pans down.
  3. If a user pans up when the secondary view is visible, that view is hidden.
  4. (non-functional) It should be easy to maintain each of the views via Interface Builder (IB).

Create the views

I like to start an application by creating the flow in IB. Start with an empty view controller in IB. Create a UIScrollView which takes up the entire screen. If using auto constraints, you can set the edges of the scroll view to match the containing view.

Next, create two container views inside the scroll view. The secondary view should be the top-most view, with the primary view right underneath. The constraints of the primary view should be set to match the size of the scroll view (so that it takes up the entire screen). As we're testing this, it is helpful to set the background colors of the embed views to different colors to get a visual indicator.

IMAGE

Setup panning

Drag a UIPanGestureRecognizer onto the View Controller, and create an IBOutlet for it in the View Controller code. Create an IBAction with the following signature:

(IBAction) didPan: (UIPanGestureRecognizer*) recognizer

CTRL+drag in IB from the UIPanGestureRecognizer to the View Controller to hook up the IBAction. Now we're ready for the final code changes.

Panning

We will wait until someone pans with a certain speed (you can calibrate this as desired--I started with 2000). Assuming that only the primary view is visible, if someone pans down, we want to show the secondary view, so we subtract the height of the secondary view from the scroll view's current content offset. Otherwise, we do nothing.

If the secondary view is visible, and the user pans up, we increment the scroll view content offset by the height of the secondary view.

Finally, before the view appears, because we want to show only the primary view, we make sure that the scroll view content offset is set to the height of the secondary view to start things off.

Conclusion

That's it in a nutshell. You can view the actual code used at this repository: . See both the Main.storyboard and the ViewController.m for the actual code.

ios-top-down-splitview's People

Contributors

jamiely avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.