HJo
HJo

Reputation: 2230

Why doesn't removeFromSuperview() collapse a tableview's subview?

I have a subview in a tableView, positioned above the cells and added via the storyboard.

Here's the document outline: enter image description here

When I click the upgrade button in the UpgradeView, I called upgradeVIew.removeFromSuperview() which removed the UpgradeView as expected. However, it didn't push the cells up like I expected, and it just left a gap.

In order to rectify this, I also call upgradeView.frame.size.height = 0, which makes the cells push up as well.

Why doesn't removeFromSuperview() collapse the subview?

Upvotes: 0

Views: 203

Answers (3)

Clafou
Clafou

Reputation: 15400

UITableView manages its own direct view hierarchy (not your custom cells, but the table headers, footers, sections, rows of cells which it creates and manages and for you). It isn't designed to respond to external changes to this view hierarchy. So it simply doesn't notice that you removed the table header view from its parent view.

However as pointed out in another reply, if you set tableHeaderView to nil, which uses the documented way to change the table header, then the UITableView will respond accordingly.

Upvotes: 1

LOKESH KUMAR PEDDA
LOKESH KUMAR PEDDA

Reputation: 437

That view is considered as a tableview header so if you make it nil then it will automatically move up.

tableView.tableHeaderView = nil

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100533

removeFromSuperview doesn't make the collapse , what does is hooking the height constraint and make it's constant = 0

self.upgradViewHeightcon.constant = 0

self.view.layoutIfNeeded()

 upgradeVIew.clipsToBounds = true

Upvotes: 1

Related Questions