简体   繁体   中英

Why are tableview section header views appearing over the table header view?

I have a UITableViewController whose header I made 'static' or 'floating' through the following code:

//MARK: Scroll view functions
override func scrollViewDidScroll(scrollView: UIScrollView) {
    //Makes sure the header does not scroll with the table view
    var headerRect = CGRect(x: 0, y: -kTableHeaderHeight, width: tableView.bounds.width, height: kTableHeaderHeight)
    headerRect.origin.y = tableView.contentOffset.y
    headerView.frame = headerRect
}

and that I setup in the following way in viewDidLoad() :

override func viewDidLoad() {     
        kTableHeaderHeight = self.view.frame.height/3
        headerView = tableView.tableHeaderView
        tableView.tableHeaderView = nil
        self.view.addSubview(headerView)
        tableView.contentInset = UIEdgeInsets(top: kTableHeaderHeight, left: 0, bottom: 0, right: 0)
        tableView.contentOffset = CGPoint(x: 0, y: -kTableHeaderHeight)
}

For some reason, scrolling the table view now causes the table view section headers to appear over the table view when scrolling. Is there any way of stopping this behavior?

Portrayed below is the table view header in light gray and the section headers in dark grey. Shouldn't section 0 be 'behind' the table view header? How can I achieve this?

在此输入图像描述 I've provided the full code below as well: import UIKit

class TableViewController: UITableViewController {

private var kTableHeaderHeight: CGFloat = CGFloat()
var headerView: UIView!

// Setting up the table header view
override func viewDidLoad() {
    super.viewDidLoad()
    initialSetup()
}

//MARK: Table view functions
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("eCell")! as UITableViewCell
    return cell
}

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

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

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    // Adding table view header separator
    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.contentView.backgroundColor = UIColor(red: 38/255, green: 38/255, blue: 38/255, alpha:1) //38
    header.textLabel?.font = UIFont.preferredFontForTextStyle(UIFontTextStyleCaption1) //headline

    // Adding cell separator
    let separator = UIView(frame: CGRectMake(15, 0, self.view.frame.width,1))
    separator.backgroundColor = UIColor(red: 57/255, green: 57/255, blue: 57/255, alpha: 1)
    header.addSubview(separator)

    header.textLabel!.textColor = UIColor(red: 131/255, green: 131/255, blue: 131/255, alpha: 1)
    self.tableView.sendSubviewToBack(view)
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Section " + String(section)
}

//MARK: Scroll view functions
override func scrollViewDidScroll(scrollView: UIScrollView) {
    updateHeaderView()
}

//MARK: Private Functions
func initialSetup() {
    kTableHeaderHeight = self.view.frame.height/3
    headerView = tableView.tableHeaderView
    tableView.tableHeaderView = nil
    self.view.addSubview(headerView)
    tableView.contentInset = UIEdgeInsets(top: kTableHeaderHeight, left: 0, bottom: 0, right: 0)
    tableView.contentOffset = CGPoint(x: 0, y: -kTableHeaderHeight)

}

func updateHeaderView() {
    //Makes sure the header does not scroll with the table view
    var headerRect = CGRect(x: 0, y: -kTableHeaderHeight, width: tableView.bounds.width, height: kTableHeaderHeight)
    headerRect.origin.y = tableView.contentOffset.y
    headerView.frame = headerRect
}

}

So I finally figured it out and I'm somewhat ashamed I didn't think of this earlier. Anyway, adding the following code fixed it:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    self.tableView.bringSubviewToFront(headerView)
}

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