PagingMenuController icon indicating copy to clipboard operation
PagingMenuController copied to clipboard

Create menu dynamically

Open staticdreams opened this issue 9 years ago • 7 comments

I'm using the latest (protocol oriented version) and I'd like to know if there is a way to generate a menu given an array of viewControllers? I looked into it already and it seems there is no easy way... with creating structs for every single item and its internal content..

Thanks

staticdreams avatar Jun 28 '16 07:06 staticdreams

Why don't you define your own initializer in struct like below?

    struct MenuItemOptions: MenuItemViewCustomizable {
        private let title: String

        var displayMode: MenuItemDisplayMode {
            let title = MenuItemText(text: self.title)
            let description = MenuItemText(text: String(self))
            return .MultilineText(title: title, description: description)
        }

        init(title: String) {
            self.title = title
        }
    }

You can set the content later by calling setup method.

kitasuke avatar Jun 29 '16 23:06 kitasuke

Could you give an example? I am using storyboard version. I need to add a viewcontrollers to the last. Is it a way to let the page menu roll the last after I add another view controller dynamically ?

ZZHHAANNGG avatar Jun 30 '16 21:06 ZZHHAANNGG

+1...Interesting Question.

thihaaung6245 avatar Jul 01 '16 07:07 thihaaung6245

Hi, as a workaround for this I followed @kitasuke recommendation and implemented as follows :


struct customPagingMenuController: PagingMenuControllerCustomizable {

    let controllers: [UIViewController]
    let options: [MenuItemViewCustomizable]

    var componentType: ComponentType {
        let menuOptions = MenuOptions(options: self.options)
        return .all(menuOptions: menuOptions, pagingControllers: self.controllers)
    }

    struct MenuOptions: MenuViewCustomizable {

        private let options: [MenuItemViewCustomizable]

        var displayMode: MenuDisplayMode {
            return .standard(widthMode: .flexible, centerItem: false, scrollingMode: .scrollEnabled)
        }
        var itemsOptions: [MenuItemViewCustomizable] {
            return self.options
        }

        init(options: [MenuItemViewCustomizable]) {
            self.options = options
        }
    }

    init(controllers: [UIViewController], options: [MenuItemViewCustomizable]) {
        self.controllers = controllers
        self.options = options
    }
}

struct customMenuItemView: MenuItemViewCustomizable {

    private let title: String

    var displayMode: MenuItemDisplayMode {
        let title = MenuItemText(text: self.title)
        return .text(title: title)
    }

    init(title: String) {
        self.title = title
    }
}

And once your custom logic finishes ( e.g. consuming a web service ) you properly create your Element's Arrays and pass them as parameter when initializing.

let pagingMenuController = self.childViewControllers.first as! PagingMenuController
let options: PagingMenuControllerCustomizable
options = customPagingMenuController(controllers: <here your UIViewcontrollers array>, options: <here your MenuItems array>)
pagingMenuController.setup(options)

You could also modify your Elements Arrays later by adding or removing items then you would just have to call once again

pagingMenuController.setup(options)

CoderPug avatar Oct 14 '16 03:10 CoderPug

CoderPug's workaround works fine except that the view controllers don't get deinited and causes memory leaks. Anyone has suggestions?

shawn-shen avatar Nov 30 '16 01:11 shawn-shen

@CoderPug , I am trying to do the same thing...the problem is, I only want to use same UITableViewController for each dynamic tab with different names. Any help?

thihaaung6245 avatar Feb 20 '17 04:02 thihaaung6245

@CoderPug's workaround works fine, thanks.

MahmoudTarek avatar Mar 07 '17 12:03 MahmoudTarek