为peek view添加显示标题

看到一些APP peek操作的时候会显示标题,模仿一下喽.

在玩APP的时候发现一些APP的peek出来的页面不太一样,显示的是有标题的,比如知乎首页和微博评论的按钮peek操作:


weibo_peek
weibo_pop
zhihu_peek
weibo_pop


简单分析一下:微博应该是用了navigation controller,而知乎则在pop的页面多了一个top视图,当peek的时候,可以看到多了一个标题的视图,真是简单粗暴.
实现一个微博的效果,简单Swift写一下:
peek页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import UIKit
class ViewController: UIViewController {
internal let tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.backgroundColor = UIColor.greenColor()
tableView.delegate = self;
tableView.dataSource = self;
tableView.tableFooterView = UIView()
view .addSubview(tableView)
}
}
extension ViewController : UITableViewDelegate,UITableViewDataSource,UIViewControllerPreviewingDelegate {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var tableViewCell = tableView.dequeueReusableCellWithIdentifier("tableviewcell")
if tableViewCell == nil {
tableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "tableviewcell")
tableViewCell?.textLabel?.text = ("第" + "\(indexPath.row)" + "行")
}
if traitCollection.forceTouchCapability == UIForceTouchCapability.Available {
registerForPreviewingWithDelegate(self, sourceView: tableViewCell!)
}
return tableViewCell!
}
/// UIViewControllerPreviewingDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
navigationController?.pushViewController(DetailViewController(), animated: true)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
/// 预览的应该包装一个UINavigationController,才会有title
let navc = UINavigationController()
navc.addChildViewController(DetailViewController())
return navc;
}
func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
/// pop的页面是UINavigationController的第一个子控制器
showViewController(viewControllerToCommit.childViewControllers[0], sender: self)
}
}

pop的页面:

1
2
3
4
5
6
7
8
9
10
11
12
import UIKit
class DetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = UIColor.cyanColor()
title = "评论"
}
}

这样就可以了.