测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新版本 | 2015年9月 |
SPM支持 SPM | ✗ |
由 Seliverstov Evgeney 维护。
SEPageViewWithNavigationBar 是 UIViewController 的一个子类,其中包含 UIPageViewController,并在滑动页面时改变导航栏标题,类似于 Twitter 应用。使用 Swift 编写,需要 iOS 7 或更高版本。
只需创建 SEPageViewWithNavigationBar 的实例,并将其视图控制器设置到 viewControllers
属性。注意,SEPageViewWithNavigationBar 应嵌入到 UINavigationController 中。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc1 = storyboard.instantiateViewControllerWithIdentifier("vc1")
let vc2 = storyboard.instantiateViewControllerWithIdentifier("vc2")
let vc3 = storyboard.instantiateViewControllerWithIdentifier("vc3")
let pagedViewController = SEPageViewWithNavigationBar()
pagedViewController.viewControllers = [vc1, vc2, vc3]
self.window?.rootViewController = UINavigationController(rootViewController: pagedViewController)
1) 在 Storyboard 中拖拽一个 UIViewController。
2) 将其放置在包含 UINavigationController 的层级中。
3) 在 Identity Inspector 中将自定义类设置为 SEPageViewWithNavigationBar
。
4) 在这个 UIViewController 和那些应该是页面的视图控制器之间添加自定义 segue。
5) 在每个 segue 的 Attributes Inspector 中将自定义类设置为 SEPageViewSegue
,并将其身份设置为 SEPage
。
SEPageViewWithNavigationBar 允许您更改以下属性
titleLabelFont : UIFont
标题标签的字体,默认值为 UIFont.systemFontOfSize(16)。
titleLabelTextColor : UIColor
标题标签的颜色,默认值为 UIColor.whiteColor()。
pageIndicatorTintColor : UIColor
页面指示器的着色,默认值为 UIColor(white: 1.0, alpha: 0.4)。
currentPageIndicatorTintColor : UIColor
当前页面指示器的着色,默认值为 UIColor.whiteColor()。
默认情况下,导航栏标题显示当前视图控制器的标题。实际上,SEPageViewWithNavigationBar 中的导航栏标题视图是 UICollectionView,因此您可以通过以下函数定义您自己的 UICollectionViewCell 并根据需要对其进行自定义
setCustomTitle(cellClass cellClass: AnyClass, delegateCallback: ((titleCell: UICollectionViewCell, currentPage: Int) -> (UICollectionViewCell)))
例如,如果您需要在标题标签旁边显示图片
class MyCustomCell: UICollectionViewCell
{
var textLabel : UILabel!
var imageView : UIImageView!
override init(frame: CGRect)
{
super.init(frame: frame)
textLabel = UILabel()
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.font = UIFont(name: "HelveticaNeue-Light", size: 20)
textLabel.textColor = UIColor.whiteColor()
self.contentView.addSubview(textLabel)
imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(imageView)
let space1 = UIView()
space1.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(space1)
let space2 = UIView()
space2.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(space2)
let views = ["textLabel" : textLabel, "imageView" : imageView, "space1" : space1, "space2" : space2]
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[space1(>=0)][imageView]-4-[textLabel][space2(==space1)]|", options: .DirectionLeftToRight, metrics: nil, views: views))
self.contentView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .CenterY, relatedBy: .Equal, toItem: self.contentView, attribute: .CenterY, multiplier: 1, constant: 0))
self.contentView.addConstraint(NSLayoutConstraint(item: textLabel, attribute: .CenterY, relatedBy: .Equal, toItem: self.contentView, attribute: .CenterY, multiplier: 1, constant: 0))
}
convenience init()
{
self.init(frame:CGRectZero)
}
}
self.window?.rootViewController = UINavigationController(rootViewController: pagedViewController)
pagedViewController.setCustomTitle(cellClass: MyCustomCell.self) { (titleCell: UICollectionViewCell, pageIndex: Int) -> (UICollectionViewCell) in
let cell = titleCell as! MyCustomCell
let titles = ["Explore", "Favorites", "Random"]
let images = [UIImage(named: "Explore"), UIImage(named: "Favorites"), UIImage(named: "Random")]
cell.textLabel.text = titles[pageIndex]
cell.imageView.image = images[pageIndex]
return cell
}
结果
只需将 SEPageViewWithNavigationBar.swift
拖动到您的项目中即可。
The MIT License (MIT)
Copyright (c) 2015 Seliverstov Evgeney
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.