简体   繁体   中英

UiView fixed on top of UiTableViewController

I need to put an UIView fixed on top of UITableViewController (like a header). I've tried this:

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

But it does not work and I don't know why. Someone have any idea?

This can not be done, one way to accomplish this is to add the UITableViewController inside UIContainerView So the structure will be as follows:

ViewController1 contains a UIContainerView this container view has embedded segue to your tableViewController.

Then you can add the view to the ViewController1.

Why do you actually use UITableViewController instead of UIViewController with a tableView inside?

Maybe you should add your header view first then add you tableview depending on the header's frame.

for example: `import UIKit

class 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
}

} `

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM