diff --git a/.DS_Store b/.DS_Store index 3e4c1ac..be988d4 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/SideMenuController.podspec b/SideMenuController.podspec index 1c5873e..34d92c5 100644 --- a/SideMenuController.podspec +++ b/SideMenuController.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'SideMenuController' - s.version = '0.2.3' + s.version = '0.2.4' s.license = 'MIT' s.summary = 'Fully customisable and easy to use side menu controller written in Swift.' s.description = 'SideMenuController is a custom container view controller written in Swift which will display the main content within a center panel and the secondary content (option menu, navigation menu, etc.) within a side panel when triggered. The side panel can be displayed either on the left or on the right side, under or over the center panel.' diff --git a/Source/UIKitExtensions.swift b/Source/UIKitExtensions.swift index aa6e433..529c08a 100644 --- a/Source/UIKitExtensions.swift +++ b/Source/UIKitExtensions.swift @@ -48,23 +48,37 @@ public extension UINavigationController { button.setImage(image, for: .normal) button.addTarget(sideMenuController, action: #selector(SideMenuController.toggle), for: UIControlEvents.touchUpInside) + if SideMenuController.preferences.drawing.sidePanelPosition.isPositionedLeft { + let newItems = computeNewItems(sideMenuController: sideMenuController, button: button, controller: self.topViewController, positionLeft: true) + self.topViewController?.navigationItem.leftBarButtonItems = newItems + } else { + let newItems = computeNewItems(sideMenuController: sideMenuController, button: button, controller: self.topViewController, positionLeft: false) + self.topViewController?.navigationItem.rightBarButtonItems = newItems + } + + completion?(button) + } + + private func computeNewItems(sideMenuController: SideMenuController, button: UIButton, controller: UIViewController?, positionLeft: Bool) -> [UIBarButtonItem] { + + var items: [UIBarButtonItem] = (positionLeft ? self.topViewController?.navigationItem.leftBarButtonItems : + self.topViewController?.navigationItem.rightBarButtonItems) ?? [] + + for item in items { + if let button = item.customView as? UIButton, + button.allTargets.contains(sideMenuController) { + return items + } + } + let item:UIBarButtonItem = UIBarButtonItem() item.customView = button let spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil) spacer.width = -10 - if SideMenuController.preferences.drawing.sidePanelPosition.isPositionedLeft { - var items = self.topViewController?.navigationItem.leftBarButtonItems ?? [] - items.append(contentsOf: [spacer, item]) - self.topViewController?.navigationItem.leftBarButtonItems = items - } else { - var items = self.topViewController?.navigationItem.rightBarButtonItems ?? [] - items.append(contentsOf: [spacer, item]) - self.topViewController?.navigationItem.rightBarButtonItems = [spacer, item] - } - - completion?(button) + items.append(contentsOf: positionLeft ? [spacer, item] : [item, spacer]) + return items } }