DHSidebarViewController is a sidebar navigation controller for iOS which supports responsive sliding gestures and custom views.
It presents a root view controller which can be slid to the right by the user to display a sidebar underneath. The sliding can be performed by a responsive pan gesture or by calling a method on the sidebar controller.
The root and sidebar view controllers can be any view controllers you want. You aren't limited to a table view in the sidebar, for example.
DHSidebarViewController operates according to Apple's guidelines for Custom Container View Controllers.
It was created by DesignHammer for the app ExTimer.
- iOS 5.0+
- Xcode 4.4+ (for ARC support and auto-synthesis)
Create a new DHSidebarViewController and pass in a view controller for the root and one for the sidebar. Then, set it as your application's window's rootViewController.
Here's an example in applicationDidFinishLaunching:
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
UIViewController* rootViewController = [mainStoryboard instantiateInitialViewController];
UIViewController* sidebarViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"SidebarViewController"];
DHSidebarViewController* sidebarVC = [[DHSidebarViewController alloc] initWithRootViewController:rootViewController sidebarViewController:sidebarViewController];
self.window.rootViewController = sidebarVC;
You can change the rootViewController or the sidebarViewController at any time.
The sidebar can be opened and closed with just a swipe gesture, however most apps will also want to include a dedicated button for this.
The simplest approach is to add a button to the root view controller which calls toggleSidebar
on the sidebar view controller.
If you need to explicitly set the sidebar to be open or closed you can call openSidebar
and closeSidebar
respectively.
Typically, some part of the rootViewController will always be visible at the right edge of the screen. However, sometimes you may wish to move it completely off of the screen temporarily so the user can focus on a task in the sidebar.
This is easily accomplished. Calling hideRootViewController
will move the rootViewController completely off the right edge of the screen.
Calling showRootViewController
will return it to the open and visible position.
A common use case for this would be having a tableView in the sidebar which the user can edit. When the user begins editing the tabeView, call hideRootViewController
. The user now has the full screen to work in, reorder items, etc. and is prevented from accidentally closing the sidebar.
Then, when the user stops editing, call showRootViewController
to restore its original position.
The sidebar controller is customizable. You can set the following properties:
If YES, the user will be able to open and close the sidebar by swiping the root view controller.
Default is YES.
This is a float value that represents the distance in points that a user needs to swipe in order to commit the open or close gesture.
Try different values and see what feels best for your app.
Default is 50.
When the sidebar controller is open (the sidebar is showing) an overlay view is placed on top of the rootViewController. This view intercepts touches so the user can quickly tap to close the sidebar.
overlayColor determines the color of this view and overlayOpacity determines it's alpha level when shown. The sidebar controller will animate the opacity as needed.
Default is black with an opacity of 0.2.
Set overlayColor to [UICColor clearColor]
to not show anything.
When open, the rootViewController will normally still be visible on the right edge of the screen. openOffset controls how far from the right edge of the screen the root view will appear.
Default is 55.
Contributions are welcome.
- Fork it.
- Create a branch (git checkout -b my_markup)
- Commit your changes (git commit -am "Fixed all the bugs")
- Push to the branch (git push origin my_markup)
- Open a Pull Request
Attribution is not required but we sure would love to know if you make something cool with this.
Licensed under the MIT License.