Create menu dynamically
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
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.
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 ?
+1...Interesting Question.
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's workaround works fine except that the view controllers don't get deinited and causes memory leaks. Anyone has suggestions?
@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?
@CoderPug's workaround works fine, thanks.