【问题标题】:UiView fixed on top of UiTableViewControllerUiView 固定在 UiTableViewController 之上
【发布时间】:2016-09-08 17:17:47
【问题描述】:

我需要将一个 UIView 固定在 UITableViewController 的顶部(如标题)。我试过这个:

override func scrollViewDidScroll (scrollView: UIScrollView)  {
     var fixedFrame: CGRect = self.uiTopView.frame;
     fixedFrame.origin.y = scrollView.contentOffset.y;
     self.uiTopView.frame = fixedFrame;
}

但它不起作用,我不知道为什么。有人知道吗?

【问题讨论】:

  • 作为测试,将 66 添加到 y 值。这有帮助吗?
  • 您是否通过storyboard 添加了您的tableViewController?如果是这样,那么您可以在 tableView 的顶部添加 UIView
  • rmaddy,谢谢您的回复。它没有造成任何影响。
  • 桑托什,谢谢您的回复。我做到了。问题是表格视图顶部的 UIView 不固定。如果我滚动屏幕,顶部的 UIView 也会滚动。我不想要那个
  • 您的表格是包含多个部分还是仅包含 1 个部分?

标签: ios swift uitableview uiview


【解决方案1】:

这个是不行的,一种方法是在UIContainerView里面添加UITableViewController 所以结构如下:

ViewController1 包含一个UIContainerView 此容器视图已嵌入 segue 到你的 tableViewController。

然后您可以将视图添加到 ViewController1。

【讨论】:

  • 说真的!?这太令人沮丧了。无论如何,非常感谢您的回复。我会试试你的建议。
【解决方案2】:

为什么你实际上使用 UITableViewController 而不是 UIViewController 里面有一个 tableView?

也许你应该先添加你的标题视图,然后根据标题的框架添加你的表格视图。

例如:`import UIKit

类 ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { var fixedLabel : UILabel! var tableView : UITableView!

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
     self.tableView.frame = CGRectMake(0, self.fixedLabel.frame.maxY, self.view.frame.width, self.view.frame.height-70)
     self.fixedLabel.frame = CGRectMake(0,0,self.view.bounds.width,70)
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.fixedLabel = UILabel()
    self.fixedLabel.backgroundColor = UIColor.blueColor()
    self.fixedLabel.text = "This is a fixedLabel"
    self.fixedLabel.textAlignment = .Center

    self.tableView = UITableView()
    self.tableView.delegate = self
    self.tableView.dataSource = self

    self.view.addSubview(fixedLabel)
    self.view.addSubview(tableView)

}

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


   var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell")

    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
    }

    cell?.textLabel?.text = "Your text"
    return cell!
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}

} `

【讨论】:

  • 谢谢你,Mat Grlt。事实上,在我的案例中使用 UiTableViewController 是有原因的。我正在寻找一些不会改变太多代码的解决方案。但是,我认为这是不可能的。无论如何,非常感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多