as diu
as diu

Reputation: 1090

How to add a floating button in UITableViewController which does not scroll when table is scrolled?

Currently there is a UITableViewContoller with many sections and rows.

What is the best way to add a button which floats on top of the table view. This button should not scroll when the cells are scrolled.

Currently I have the following code and with this the button still scrolls:

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.addSubview(viewForFooter)
}

the viewForFooter is a separate view which contains the button which needs to be floating. Thank you. Any help would be greatly appreciated.

Upvotes: 1

Views: 1156

Answers (4)

Martin Muldoon
Martin Muldoon

Reputation: 3438

I had exactly this problem. I'm using Interface Builder and Auto Layout to place it so, it is not a programtic solution like the ones suggest by my colleagues:

The button must be placed at the same level as the UITableView. Take care where you place it in the hierarchy as depicted below: It can not be below the Table View in the hierarchy.

enter image description here

Upvotes: -1

Akii_Dev
Akii_Dev

Reputation: 53

    override func viewWillAppear(_ animated: Bool) {
        self.btnPickup = UIButton()
        self.btnPickup?.frame = CGRect(x: self.view.frame.size.width - 75, y: self.view.frame.size.height - 150, width: 50, height: 50)
//        self.btnPickup?.setTitle("+", for: .normal)
        self.btnPickup?.setBackgroundImage(#imageLiteral(resourceName: "add_user"), for: .normal)
        self.btnPickup?.titleLabel?.textAlignment = .center
        self.btnPickup?.titleLabel?.font = UIFont(name: (self.btnPickup?.titleLabel?.font.fontName)!, size: 50)
//        self.btnPickup?.layer.borderColor = UIColor.black.cgColor
        self.btnPickup?.backgroundColor = UIColor.white
//        self.btnPickup?.layer.borderWidth = 1
        self.btnPickup?.layer.cornerRadius = 25
        self.btnPickup?.clipsToBounds = true

//        self.btnPickup?.setTitleColor(UIColor.white, for: .normal)
        self.btnPickup?.addTarget(self, action: #selector(DirectoryViewController.btnTapped(sender:)), for: .touchUpInside)

        print(self.navigationController?.view.subviews.count ?? "error")
        self.navigationController?.view.superview?.insertSubview(self.btnPickup!, at: (self.navigationController?.view.subviews.count)!)
    }

    override func viewWillDisappear(_ animated: Bool) {
        self.btnPickup?.removeFromSuperview()
    }

Upvotes: 0

DonMag
DonMag

Reputation: 77700

Here is an option if you really don't want to use a UIView with subviews...

override func viewDidLoad() {
    super.viewDidLoad()

    if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
        print("adding view-with-button to keyWindow")
        window.addSubview(viewForFooter)
        viewForFooter.topAnchor.constraint(equalTo: window.topAnchor, constant: 120).isActive = true
        viewForFooter.leftAnchor.constraint(equalTo: window.leftAnchor, constant: 40).isActive = true
    }

    // other stuff...
}

This will add the view as a subview of the "keyWindow" at 40,120, and will "hover" there while you scroll the table. I am assuming your viewForFooter is properly instantiated and you have the necessary constraints set up correctly.

Upvotes: 2

OverD
OverD

Reputation: 2632

The easiest way is to use a viewController that will contain the tableView as a subView and then You can add you floating button as a subview of the viewController

Upvotes: 0

Related Questions