在UITableView上覆盖UIView并将其粘贴在滚动导航栏下方

时间:2016-08-05 00:01:57

标签: ios objective-c uitableview uiscrollview

我正在尝试实现与此模型类似的内容:http://share.framerjs.com/16hwz94ezflt/(向下滚动)

如果向下滚动"周"视图将粘在导航栏下方。 我有一个类似的带有标题的UITableView(在"周"视图之上)。

我不清楚这个"周"视图是否是表视图的一部分。它并不重要,但我真的很好奇如何实现这样的事情。

2 个答案:

答案 0 :(得分:1)

以下是我使用自动布局约束

的方法
  1. 在UIScrollView / UITableView中保留所有 WeekView
  2. 设置滚动视图中的所有项目
  3. 将WeekView创建为UIView(或类似的东西)并赋予它以下约束:

    • A:设置为容器边距的前/后空格(可以是主视图或滚动视图)
    • B:指定静态高度约束
    • C:添加一个约束,将WeekView的顶部与其上方的视图相关联(在本例中为总SalesView),空间设置为0.此约束应使周视图遵循SaleView的位置。
    • D:添加一个约束,将WeekView的顶部与View Controller中祖父视图的顶部相关联。此约束将使WeekView保持在屏幕顶部
  4. 现在显然,最后2个约束相互冲突。你现在做的是修改约束D,使得关系不是= 0,但是> = 0.然后,将约束C的优先级设置为小于1000(默认值),比如900.所以约束C如果另一个约束(在这种情况下为约束D)需要它破坏,那么基本上可以破坏。

    这样做会告诉autolayout“好吧,只要WeekView低于顶部它很酷。它不能超过顶部。我也希望它在SalesView的位置正下方,但是如果约束D就像“嘿,退后”,我会退缩“

    这有意义吗?

答案 1 :(得分:0)

  

我不清楚这个“周”视图是否是表格视图的一部分。这并不重要,但我真的很好奇如何实现这样的事情。

当然可以!

您的实施可能取决于您使用的是UICollectionView还是UITableView

对于UITableView,示例中包含水平滚动页面的部分可能是UITableViewHeader。包含“周”视图的粘性区域是您在tableView:viewForHeaderInSection:中定义的自定义UITableHeaderFooterView

对于UICollectionView,涉及一些手工劳动。这个想法基本上是相同的 - 一个部分包含一个标题(UICollectionElementKindSectionHeader),它根据您的集合视图布局定义坚持导航。看看herehere的想法。

编辑:以下是在游乐场进行评估的粗略示例。

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