PageMenuController Swift。
A class of the user interface that displays the side scroll menu screen used in news applications and its contents. Since Xcode's complete set of projects are registered, you can check the operation on the simulator if you run it.
When reimplementing with Swift 3, we redesigned the class to be versatile and easy to extend. Since only the appearance of the page menu is different, we implemented a subclass of PMKPageMenuItem for each style and used it. Therefore, you can easily add custom menus.
pod 'PageMenuKitSwift'
Running Build with Xcode's Build Target with PageMenuKitFatBinary creates PageMenuKit.framework. This is set in Project of Xcode of homebrew application.
After that, write the following code and use it.
class RootViewController: UIViewController
{
var pageMenuController: PMKPageMenuController? = nil
override func viewDidLoad() {
super.viewDidLoad()
var controllers: [UIViewController] = []
let dateFormatter = DateFormatter()
for month in dateFormatter.monthSymbols {
let viewController: DataViewController = DataViewController()
viewController.title = month
controllers.append(viewController)
}
let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
/*
* Available menu styles:
* .Plain, .Tab, .Smart, .Hacka, .Ellipse, .Web, .Suite, .NetLab and .NHK
* See PMKPageMenuItem.swift in PageMenuKit folder.
*
* menuColors: [] means that we will use the default colors
*/
pageMenuController = PMKPageMenuController(controllers: controllers, menuStyle: .Smart, menuColors: [], topBarHeight: statusBarHeight,itemWidth:100)
self.addChildViewController(pageMenuController!)
self.view.addSubview(pageMenuController!.view)
pageMenuController?.didMove(toParentViewController: self)
}
}
For more detailed code look at RootViewController.swift in PageMenuKitDemo.
News path like menu screen
Gnochey like menu screen
SmartNews like menu screen
Hacka Doll like menu screen
JCnews like menu screen
JCnews website website like menu screen
NewsSuite like menu screen (background color is gradation)
Netorabo like menu screen
NHK News · Disaster Prevention like menu screen
Specifying [] for menuColors of the initializer of PMKPageMenuController sets the default color scheme. Here, an example of changing the color for each style is shown.
.Plain, .Hacka, .Ellipse, .NHK styles can specify only one color. In the following example, purple (.purple) is set to .Plain style .
pageMenuController = PMKPageMenuController(controllers: controllers, menuStyle: .Plain, menuColors: [ .purple ], topBarHeight: statusBarHeight,itemWidth:100)
.Tab, .Smart style can specify one or more colors. In the following example, red, orange, yellow, green, blue, purple are set in. Tab style . If the number of menus is larger than the number of colors, colors are applied in order.
pageMenuController = PMKPageMenuController(controllers: controllers, menuStyle: .Tab, menuColors: [ .red, .orange, .yello, .green, .blue, .purple ], topBarHeight: statusBarHeight,itemWidth:100)
As with the .Tab, .Smart style, Web styles can be specified with one or more colors. However, the background color is currently fixed. Also, in the current implementation, the color specified at the beginning is the color of the boundary line.
In the following example, red, orange, yellow, green, blue, purple are set for Web style . If the number of menus is larger than the number of colors, colors are applied in order.
pageMenuController = PMKPageMenuController(controllers: controllers, menuStyle: .Web, menuColors: [ .red, .orange, .yello, .green, .blue, .purple ], topBarHeight: statusBarHeight,itemWidth:100)
Although there is only one color that can be specified with the Suite style, the color of the indicator is only changed at present. In the following example, blue (.blue) is set for the Suite style .
pageMenuController = PMKPageMenuController(controllers: controllers, menuStyle: .Suite, menuColors: [ .blue ], topBarHeight: statusBarHeight,itemWidth:100)
Only one color can be specified in .NetLab style. Currently, it affects the color of characters when not selected. In the following example, red ( . Red) is set for .NetLab style .
pageMenuController = PMKPageMenuController(controllers: controllers, menuStyle: .NetLab, menuColors: [ .red ], topBarHeight: statusBarHeight,itemWidth:100)
It is also possible to use Delegate which is called when switching pages.
pageMenuController?.delegate = self
Add the above description and implement the following methods as necessary. At the moment, I use it only when displaying a .Hacka style badge.
public protocol PMKPageMenuControllerDelegate: class
{
// Called before switching by swipe operation on page screen
func pageMenuController ( _ pageMenuController : PMKPageMenuController, willMoveTo viewController : UIViewController, at menuIndex : Int )
// Called when page switching is complete
func pageMenuController(_ pageMenuController: PMKPageMenuController, didMoveTo viewController: UIViewController, at menuIndex: Int)
// Called when creation of menu item etc. is completed
func pageMenuController ( _ pageMenuController : PMKPageMenuController, didPrepare menuItems : [PMKPageMenuItem])
// Called when the menu is tapped
func pageMenuController(_ pageMenuController: PMKPageMenuController, didSelect menuItem: PMKPageMenuItem, at menuIndex: Int)
}
pageMenuController?.movePageTo(index:0)
Also look at the implementation of PageMenuKit, the user interface of Qiita's news applications . It also describes how to implement custom menus.
- Swift 4
- iOS 11.0 or later
- Xcode 9.0 or later
- .Suite, .Web and .NetLab style customization method
Since there is no Cocaopod installation option and no English Readme for the original repo PageMenuKitSwift, I have create an Cocaopod and English Readme. All credits goes to original repo PageMenuKitSwift and owner Kouichi ABE.