![](/img/trans.png)
[英]UIView with labels at top of UITableviewcontroller Swift
[英]UiView fixed on top of UiTableViewController
我需要將固定的UIView放在UITableViewController的頂部(如標頭)。 我已經試過了:
override func scrollViewDidScroll (scrollView: UIScrollView) {
var fixedFrame: CGRect = self.uiTopView.frame;
fixedFrame.origin.y = scrollView.contentOffset.y;
self.uiTopView.frame = fixedFrame;
}
但這不起作用,我也不知道為什么。 有人知道嗎
無法做到這一點,一種實現方法是在UIContainerView
內添加UITableViewController,因此結構如下:
ViewController1包含一個UIContainerView
此容器視圖已將segue嵌入到tableViewController中。
然后,您可以將視圖添加到ViewController1。
為什么您實際上在內部使用tableView而不是UITableViewController而不是UIViewController?
也許您應該先添加標題視圖,然后再根據標題框架添加表視圖。
例如:`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
}
}`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.