Home > Mobile >  How to add UIView above a tableView Swift(iOS)
How to add UIView above a tableView Swift(iOS)

Time:01-03

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.

enter image description here

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

CodePudding user response:

Try to create custom UIView for header section and add "RankView" to this custom view and setup clipToBounds = false (because you RankView will be located out of bounds header view). Then override method UITableView Delegate "viewForHeaderInSection" (https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections) and return you custom header view.

CodePudding user response:

add the below code to viewDidLoad() function of your project's class

let anotherview = UIView()
    anotherview.frame = CGRect(x: 150, y: 200, width: 190, height: 50)
    anotherview.backgroundColor = .red
    self.view.addSubview(anotherview)
    

and you can add or change it as per your need.

UPVOTE if helpful!

  • Related