I have crate one demo navigation drawer by pre-setting a controller with custom animation from left to right. And when we dismiss the controller we apply animation from right to left. It works though but the drawer animation does not look as good as native android drawer. So can you tell me what is the best way to achieve this. Although I don't want to depend upon any third party for this functionality.
@IBAction func actionMenu(_ sender: Any) {
let controler:DrawerController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DrawerController")
as! DrawerController
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionDefault)
view.window!.layer.add(transition, forKey: kCATransition)
present(controler, animated: false) {
controler.drawerView.backgroundColor = UIColor.black
controler.drawerView.alpha = 0.6
}
}
Below is storyboard screenshot
Figure 3. An open drawer displaying a navigation menu. The drawer icon is displayed on all top-level destinations that use a DrawerLayout . To add a navigation drawer, first declare a DrawerLayout as the root view.
If you want to achieve this without using any third-arty lib it is achievable by this approach.
Add Side TableViewController to container view using segue
Set the width of Container view as needed.
Add constraints to container view leading, top, bottom and constant width.
Create outlet of trailing constraint in view and toggle its constant from MainViewController value minus Width value to hide and 0 to show.
** MainViewController**
import UIKit
class MainViewController: UIViewController {
@IBOutlet var leadingConstraint: NSLayoutConstraint!
@IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func openView(_ sender: Any) {
if leadingConstraint.constant == -250{
leadingConstraint.constant = 0
}
else{
leadingConstraint.constant = -250
}
UIView.animate(withDuration: 0.4) {
self.view.layoutIfNeeded()
}
}
}
SlideTableViewController
import UIKit
class SlideTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var dataItems: [String] = ["item1", "item2", "item3", "item4"]
@IBOutlet weak var tabelView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tabelView.delegate = self
tabelView.dataSource = self
}
// MARK: - Table view data source
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = dataItems[indexPath.row]
return cell
}
}
Result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With