Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] SideMenu 사용 방법 (SideMenu 프레임워크) 본문

iOS 응용 (swift)

[iOS - swift] SideMenu 사용 방법 (SideMenu 프레임워크)

jake-kim 2020. 12. 19. 21:45

프레임워크

pod 'SideMenu'

SideMenu로 사용 할 화면 추가

  • 코드로 화면 초기화를 위해, 관련 protocol정의
import UIKit

protocol StoryboardInitializable {
    static var storyboardName: String { get set }
    static var storyboardID: String { get set }
    static func instantiate() -> Self
}

extension StoryboardInitializable where Self: UIViewController {

    static func instantiate() -> Self {
        if #available(iOS 13.0, *) {
            let storyboard = UIStoryboard(name: storyboardName, bundle: Bundle.main)
            return storyboard.instantiateViewController(identifier: storyboardID) { (coder) -> Self? in
                return Self(coder: coder)
            }
        } else {
            let storyboard = UIStoryboard(name: storyboardName, bundle: Bundle.main)
            let vc = storyboard.instantiateViewController(withIdentifier: storyboardID) as! Self
            return vc
        }
    }
}
  • SideMenu로 사용할 화면 추가
import UIKit

class MenuVC: UIViewController, StoryboardInitializable {
    static var storyboardName: String = "Main"
    static var storyboardID: String = "MenuVC"

}

SideMenu 프레임워크를 사용하여 세팅

  • SideMenu화면이 세팅 될 SideMenuNavigationController 설정
  • SideMenu의 속성(닫힘, alpha, width)은 SideMenuNavigationController객체로 접근
// ViewController

    override func viewDidLoad() {
        super.viewDidLoad()

        let menuVC = MenuVC.instantiate()
        let sideMenuNav = SideMenuNavigationController(rootViewController: menuVC) // rootViewController에는 SideMenu화면 VC를 삽입
        setUpSideMenuNavigationVC(vc: self, menuNavVC: sideMenuNav)
    }

    // sideMenu속성은 SideMenuNav객체로 지정
    func setUpSideMenuNavigationVC(vc: ViewController, menuNavVC: SideMenuNavigationController) {
        menuNavVC.statusBarEndAlpha = 0
        menuNavVC.dismissOnPresent = true
        menuNavVC.dismissOnPush = true
        menuNavVC.enableTapToDismissGesture = true
        menuNavVC.enableSwipeToDismissGesture = true
        menuNavVC.enableSwipeToDismissGesture = true
        menuNavVC.sideMenuDelegate = vc
        menuNavVC.menuWidth = 238
        menuNavVC.presentationStyle = .menuSlideIn
        SideMenuManager.default.leftMenuNavigationController = menuNavVC
        SideMenuManager.default.leftMenuNavigationController?.setNavigationBarHidden(true, animated: true)
    }
  • delegate: SideMenu밑에 깔려있는 ViewController에 삽입 (위에서 menuNavVC.sideMenuDelegate = vc)부분
extension ViewController: SideMenuNavigationControllerDelegate {

    func sideMenuWillAppear(menu: SideMenuNavigationController, animated: Bool) {
    }

    func sideMenuDidAppear(menu: SideMenuNavigationController, animated: Bool) {

    }

    func sideMenuWillDisappear(menu: SideMenuNavigationController, animated: Bool) {
    }

    func sideMenuDidDisappear(menu: SideMenuNavigationController, animated: Bool) {

    }
}
  • 사용: SideMenuManager.default.leftMenuNavigationController로 SideMenuNav 접근
    @IBAction func sideMenu(_ sender: Any) {
        let sideMenu = SideMenuManager.default.leftMenuNavigationController!
        present(sideMenu, animated: true)
    }

* 전체 소스코드 : github.com/JK0369/SideMenuSample

Comments