Is there a way to implement Slide Sidebar Menu (Like Facebook app) in IOS Swift without any third party library? I look for solutions but I only founded this feature implemented in Objective-C.
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- Using if let syntax in switch statement
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
- How can I vertically align my status bar item text
- Adding TapGestureRecognizer to UILabel in Swift
- Attempt to present UIAlertController on View Contr
- Swift - Snapshotting a view that has not been rend
Here is a small example of how I do it, this drawer control has uiviewcontrollers for left, center, and right. This one works like the Slack IPad app where either one side or the other is open.
Source here.
Image here.
Video here.
Here's another SideMenu library I made to add to the mix: https://github.com/jonkykong/SideMenu.
I looked all over the place for a Swift menu solution that didn't require a library. Ended up creating a tutorial of my own, which is fairly similar to Fengson's approach:
Here are some of the main points:
scrollview
which will handle the menu sliding motion, along with the pan gesturecontainer
views side by side, inside thescrollview
. Containers allow you to embed top level controllers.scrollview
, setpaging enabled
but disablebounces
. This will coerce the slideout into an open or closed stateembed
aUITableViewController
in the left containerembed
aUITabBarController
in the right containercontainer
, add a runtime attributelayer.shadowOpacity
of 0.8. This gives you a free shadow separator without any code.NSNotificationCenter
to communicate with thescrollview
scrollView.setContentOffset.x
to take care of the actual opening and closing of the menuHere's a working example project of a tab bar application with a left slideout menu, including screenshots and instructions.
https://github.com/ThornTechPublic/LeftSlideoutMenu
Along with a more generic explanation of how it works:
http://www.thorntech.com/2015/06/want-to-implement-a-slideout-menu-in-your-swift-app-heres-how/
I think using Custom View Controller Transitions is a solid approach for making slideout menus:
Custom View Controller Transitions are difficult to learn at first (they were for me, at least). I wrote a blog post on how to create an interactive slideout menu, and tried my best to make it as easy to understand as possible.
You can also jump straight into the code on GitHub.
At a very high level, this is how it works:
UIViewControllerAnimatedTransitioning
protocol. You use a snapshot to represent the main view controller during animations.UIPercentDrivenInteractiveTransition
object to keep the transition in sync with the user's movements.UIViewControllerTransitioningDelegate
protocol and wires up all the custom animations and interactive transitions.I actually have a previous answer on this thread that uses a Scrollview / Container View. This approach is OK for a prototype, but runs into a lot of edge cases and bugs when making your app production ready. Spending every week responding to blog comments and fixing edge cases is what motivated me to write a second blog post on the subject.
I have implemented it in 2 ways.
First using
Scroll View
and second usingContainer Views
.You can have
TableViewController
acting as a Menu in one container and aTabBarController
with hidden tabs in a second container. Pressing aCell
in a Table View moves you to a n-th tab in Tab Bar.All you have to do is animate the top container right on button click or a gesture. It then may look like this:
What is neat is that you can then easily add cool effects such as animating UIView using spring damping by using built-in methods to give it realistic bouncy effect. You can also add shadow to your main View (not added in the picture) to make it look like a page above the menu.
I believe you can start form UISplitViewController which was drastically updated in iOS8. Watch sessions View Controller Advancements in iOS8 and Building Adaptive Apps with UIKit for details. They provide code example from second session (but not form first one :/). At this point it feels natural for me to make this kind of UI based on split view controller in iOS8.
Update: It looks like not all API they talking about are landed yet. In current beta4 mentioned in video condensesBarsOnSwipe is not presented, for example.