![](/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.