我正在尝试实现与此模型类似的内容:http://share.framerjs.com/16hwz94ezflt/(向下滚动)
如果向下滚动"周"视图将粘在导航栏下方。 我有一个类似的带有标题的UITableView(在"周"视图之上)。
我不清楚这个"周"视图是否是表视图的一部分。它并不重要,但我真的很好奇如何实现这样的事情。
答案 0 :(得分:1)
以下是我使用自动布局约束
的方法将WeekView创建为UIView(或类似的东西)并赋予它以下约束:
现在显然,最后2个约束相互冲突。你现在做的是修改约束D,使得关系不是= 0,但是> = 0.然后,将约束C的优先级设置为小于1000(默认值),比如900.所以约束C如果另一个约束(在这种情况下为约束D)需要它破坏,那么基本上可以破坏。
这样做会告诉autolayout“好吧,只要WeekView低于顶部它很酷。它不能超过顶部。我也希望它在SalesView的位置正下方,但是如果约束D就像“嘿,退后”,我会退缩“
这有意义吗?
答案 1 :(得分:0)
我不清楚这个“周”视图是否是表格视图的一部分。这并不重要,但我真的很好奇如何实现这样的事情。
当然可以!
您的实施可能取决于您使用的是UICollectionView
还是UITableView
。
对于UITableView
,示例中包含水平滚动页面的部分可能是UITableViewHeader
。包含“周”视图的粘性区域是您在tableView:viewForHeaderInSection:
中定义的自定义UITableHeaderFooterView
。
对于UICollectionView
,涉及一些手工劳动。这个想法基本上是相同的 - 一个部分包含一个标题(UICollectionElementKindSectionHeader
),它根据您的集合视图布局定义坚持导航。看看here和here的想法。
编辑:以下是在游乐场进行评估的粗略示例。
import UIKit
import Foundation
import XCPlayground
class StickyTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView!
var items = ["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"
]
let tableViewHeader = UIView()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView = UITableView(frame: self.view!.frame, style: .Plain)
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "TableViewCell")
tableViewHeader.frame = CGRectMake(0, 0, self.tableView.frame.width, 300)
tableViewHeader.backgroundColor = UIColor.blueColor()
self.tableView.tableHeaderView = tableViewHeader
self.view.addSubview(self.tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 0
default:
return items.count
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRectMake(0, 0, self.tableView.frame.width, 40))
headerView.backgroundColor = UIColor.grayColor()
let headerLabel = UILabel(frame: headerView.frame)
headerLabel.text = "Label"
headerLabel.textAlignment = .Center
headerView.addSubview(headerLabel)
switch section {
case 0:
return nil
case 1:
return headerView
default:
return headerView
}
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0:
return nil
default:
return " "
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell", forIndexPath: indexPath)
cell.textLabel?.text = "\(self.items[indexPath.row])"
return cell
}
}
let rootViewController = StickyTableViewController()
let navigationController = UINavigationController(rootViewController: rootViewController)
let page = XCPlaygroundPage.currentPage
page.liveView = navigationController.view