问题
I'm trying to get this pod working in my project and I'm stuck on understanding how this bit works :
https://github.com/uacaps/PageMenu
// Create variables for all view controllers you want to put in the
// page menu, initialize them, and add each to the controller array.
// (Can be any UIViewController subclass)
// Make sure the title property of all view controllers is set
// Example:
var controller : UIViewController = UIViewController(nibName: "controllerNibName", bundle: nil)
controller.title = "SAMPLE TITLE"
controllerArray.append(controller)
I've seen things that say I need to initialize the view controller with the storyboard ID I set in the storyboard , but when I try to do somethign like
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
But to be honest I have no idea where/when to put that/ if that's even what
The idea is to make an array of view controllers for the pagemenu to display with the tab bar up at the top, but I'm not sure how exactly to make that array from views I'm making in the storyboard.
回答1:
Here you go. PageMenu is super cool and super customizable. Have fun. Hope this helps. Let me know if you run into any other problems.
var pageMenu: CAPSPageMenu?
override func viewDidLoad() {
super.viewDidLoad()
setupPages()
}
func setupPages() {
var controllerArray: [UIViewController] = []
let firstVC = FirstViewController()
firstVC.title = "FirstOne"
let secondVC = SecondViewController()
secondVC.title = "Another One"
let thirdVC = ThirdViewController()
thirdVC.title = "And Another One"
controllerArray.append(firstVC)
controllerArray.append(secondVC)
controllerArray.append(thirdVC)
// a bunch of random customization
let parameters: [CAPSPageMenuOption] = [
.ScrollMenuBackgroundColor(UIColor.quotesBackgroundColor()),
.ViewBackgroundColor(UIColor.quotesBackgroundColor()),
.SelectionIndicatorColor(UIColor.peterRiverColor()),
.BottomMenuHairlineColor(UIColor(red: 70.0/255.0, green: 70.0/255.0, blue: 80.0/255.0, alpha: 1.0)),
.MenuHeight(40.0),
.MenuItemWidth(100.0),
.CenterMenuItems(true),
.SelectedMenuItemLabelColor(UIColor.blueColor())
]
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 0.0, self.view.frame.width, self.view.frame.height), pageMenuOptions: parameters)
self.view.addSubview(pageMenu!.view)
}
回答2:
Modification to above answer if you are using storyboardIds for viewcontroller instantiation.
var pageMenu: CAPSPageMenu?
override func viewDidLoad() {
super.viewDidLoad()
setupPages()
}
func setupPages() {
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
var controllerArray: [UIViewController] = []
let firstVC = storyboard.instantiateViewControllerWithIdentifier("FirstViewControlleridentifier") as! FirstViewController
firstVC.title = "FirstOne"
let secondVC = storyboard.instantiateViewControllerWithIdentifier("SecondViewControlleridentifier") as! SecondViewController
secondVC.title = "Another One"
let thirdVC = storyboard.instantiateViewControllerWithIdentifier("ThirdViewControlleridentifier") as! ThirdViewController
thirdVC.title = "And Another One"
controllerArray.append(firstVC)
controllerArray.append(secondVC)
controllerArray.append(thirdVC)
// a bunch of random customization
let parameters: [CAPSPageMenuOption] = [
.ScrollMenuBackgroundColor(UIColor.quotesBackgroundColor()),
.ViewBackgroundColor(UIColor.quotesBackgroundColor()),
.SelectionIndicatorColor(UIColor.peterRiverColor()),
.BottomMenuHairlineColor(UIColor(red: 70.0/255.0, green: 70.0/255.0, blue: 80.0/255.0, alpha: 1.0)),
.MenuHeight(40.0),
.MenuItemWidth(100.0),
.CenterMenuItems(true),
.SelectedMenuItemLabelColor(UIColor.blueColor())
]
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 0.0, self.view.frame.width, self.view.frame.height), pageMenuOptions: parameters)
self.view.addSubview(pageMenu!.view)
}
回答3:
Updated Swift-3 Version of David
Podfile for Swift-3
pod 'PageMenu' , :git => 'https://github.com/orazz/PageMenu'
func setupPages() {
var controllerArray: [UIViewController] = []
let firstVC = FirstVC()
firstVC.title = "First"
let secondVC = SecondVC()
secondVC.title = "Second"
let thirdVC = ThirdVC()
thirdVC.title = "Third"
controllerArray.append(firstVC)
controllerArray.append(secondVC)
controllerArray.append(thirdVC)
let parameters: [CAPSPageMenuOption] = [
.scrollMenuBackgroundColor(.blue),
.viewBackgroundColor(.white),
.selectionIndicatorColor(.white),
.bottomMenuHairlineColor(.white),
.menuHeight(40.0),
.menuItemWidth(self.view.frame.width/3),
.centerMenuItems(true),
.selectedMenuItemLabelColor(.white),
.unselectedMenuItemLabelColor(.white),
.menuMargin(0.0)
]
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRect(x:0,y:64,width:self.view.frame.width ,height:self.view.frame.height) , pageMenuOptions: parameters)
self.view.addSubview(pageMenu!.view)
}
回答4:
An updated Swift-3 version of Shrawn compatible with an iPhone-X simulator
func setupPages() {
var controllerArray: [UIViewController] = []
let firstVC = FirstVC()
firstVC.title = "First"
let secondVC = SecondVC()
secondVC.title = "Second"
let thirdVC = ThirdVC()
thirdVC.title = "Third"
controllerArray.append(firstVC)
controllerArray.append(secondVC)
controllerArray.append(thirdVC)
let parameters: [CAPSPageMenuOption] = [
.scrollMenuBackgroundColor(.blue),
.viewBackgroundColor(.white),
.selectionIndicatorColor(.white),
.bottomMenuHairlineColor(.white),
.menuHeight(40.0),
.menuItemWidth(self.view.frame.width/3),
.centerMenuItems(true),
.selectedMenuItemLabelColor(.white),
.unselectedMenuItemLabelColor(.white),
.menuMargin(0.0)
]
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRect(x:0,y:(self.navigationController?.navigationBar.frame.maxY)!,width:self.view.frame.width ,height:self.view.frame.height), pageMenuOptions: parameters)
self.view.addSubview(pageMenu!.view)}
回答5:
Or you can use this repo .It is so much simpler and easily understandable for RnD. (Swift 3 code)
https://github.com/lakshikabhardwaj/LBViewControllerCollection
let mainViewController = CPPageMenuVC(nibName: "CPPageMenuVC", bundle: nil)
let pageMenuarray :[PageModal] = [PageModal(pageTitle: "Cat", pageVC: cpCatVC),PageModal(pageTitle: "Cow", pageVC: cowCX),PageModal(pageTitle: "Chat", pageVC: cpCatVC),PageModal(pageTitle: "ElephantElephant", pageVC: elephantVC)]
pageMenuVC.pageArray = pageMenuarray
来源:https://stackoverflow.com/questions/39339898/implementing-pagemenu-initializing-view-controllers-from-storyboard