Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create navigation drawer by presenting controller?

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 enter image description here enter image description here

like image 776
TechChain Avatar asked Jun 06 '18 08:06

TechChain


People also ask

Which view is used to add the navigation drawer to an app?

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.


1 Answers

If you want to achieve this without using any third-arty lib it is achievable by this approach.

  1. Add a container view in MainViewController .
  2. Add Side TableViewController to container view using segue

  3. Set the width of Container view as needed.

  4. Add constraints to container view leading, top, bottom and constant width.

  5. 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
    }
}

enter image description here enter image description here

Result

like image 80
Rizwan Mehboob Avatar answered Nov 06 '22 20:11

Rizwan Mehboob