我添加了CAPSPageMenu。当前索引始终为0。 我想知道我点击的菜单项的索引。
我尝试下面的代码,但是当我将断点添加到此委托方法时,未调用它。
我正在使用此lib:https://github.com/PageMenu/PageMenu/blob/master/README.md
class firstVC: UIViewController,CAPSPageMenuDelegate {
var pageMenu : CAPSPageMenu?
override func viewDidLoad() {
super.viewDidLoad()
pageMenu?.delegate=self
}
func willMoveToPage(_ controller: UIViewController, index: Int){
print(index)
}
func didMoveToPage(_ controller: UIViewController, index: Int){
print(index)
}
}
更新:
@IBOutlet var optionView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
var menuTitleFont: UIFont?
if UIScreen.main.bounds.size.height == 568 {
menuTitleFont = UIFont(name: "roboto-Bold", size: 10.0)
}
else {
menuTitleFont = UIFont(name: "roboto-Bold", size: 12.0)
}
let parameters: [CAPSPageMenuOption] = [
.scrollMenuBackgroundColor(UIColor.yellow),
.viewBackgroundColor(UIColor.black),
.selectionIndicatorColor(UIColor.white),
.bottomMenuHairlineColor(UIColor.lightGray),
.selectedMenuItemLabelColor(UIColor.white),
.unselectedMenuItemLabelColor(UIColor.lightGray),
//.menuItemFont(12),
.menuHeight(50.0),
.menuItemWidth(self.view.frame.size.width/10 + 30),
.centerMenuItems(true)
]
print(pageMenu)
pageMenu!.delegate = self
var controllerArray = [UIViewController]()
if let AllVc = self.storyboard?.instantiateViewController(withIdentifier:"AllVc") {
AllVc.title = "AllVc"
controllerArray.append(AllVc)
}
if let baseVc = self.storyboard?.instantiateViewController(withIdentifier:"baseVc") {
baseVc.title = "baseVc"
controllerArray.append(baseVc)
}
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRect(x: 0.0, y:0.0, width:view.frame.width, height:self.view.frame.height), pageMenuOptions: parameters)
self.addChildViewController(pageMenu!)
self.optionView.addSubview(pageMenu!.view)
pageMenu!.didMove(toParentViewController: self)
}
这是我完整查看的已加载代码。
答案 0 :(得分:0)
在初始化CAPSPageMenu之后,需要设置委托。
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRect(x: 0.0, y:0.0, width:view.frame.width, height:self.view.frame.height), pageMenuOptions: parameters)
pageMenu!.delegate = self
print(pageMenu)