I’m trying to add a UIview above the tableView but my view is hiding behind the cells. The view should half on footer of section 0 and half on header of section 1, The empty space is tha padding for header.
I have used bringSubviewToFront method but it’s not working. Everything that I had tried are commented below.
func setupMyRankView(){
// tableView.addSubview(myRankView)
// tableView.footerView(forSection: 0)?.insertSubview(myRankView, aboveSubview: self.view)
// tableView.footerView(forSection: 0)?.bringSubviewToFront(myRankView)
self.view.bringSubviewToFront(myRankView)
// tableView.cellForRow(at: indexPath)?.insertSubview(myRankView, aboveSubview: self.view)
// tableView.cellForRow(at: indexPath)?.bringSubviewToFront(myRankView)
myRankView.myRankLabel.text = "Hello"
}
3
Answers
add the below code to viewDidLoad() function of your project’s class
and you can add or change it as per your need.
UPVOTE if helpful!
Try to create a custom UIView for the header section and add "RankView" to this custom view and set up
clipToBounds = false
(because your RankView will be located out of bounds header view). Then override method UITableView Delegate "viewForHeaderInSection" (See the official documents) and return your custom header view.As the table view manages its cells and section headers/footers, it is constantly rearranging the z-orders.
What you need to do is bring the "rank" views to the front every time the table updates itself.
One way to do that is by implementing the table view’s
scrollViewDidScroll
— this gives you the added advantage of being a good place to calculate the frame(s) for the "rank" view(s).It will go something like this:
scrollViewDidScroll
and the code will look like this:
Here’s a complete example you can try out…
Sample "Rank" view
Simple multi-line label cell
Reusable section header/footer views
Sample controller
and it looks like this when running: