简体   繁体   中英

Swift +hide or remove a UIView

I have UITableViewController , on top of it I placed an UIView and inside it there is a UICollectionView .

When collectionView data is empty I want to remove the UIView . I tried:

self.collectionView.removeFromSuperview() 

and

self.collectionView.hidden = true

These removes the UIView but there is an empty space above my tableview . How can I get rid of it?

Edit: added a photo of my storyboard. Recommended View is not inside the tableviewheader.

在此处输入图片说明

我没有尝试过,但是这段代码应该可以解决您的问题。

self.tableView.tableHeaderView?.isHidden = true

You have placed Recommended View inside the table view - not on top of it. So you could have placed it as header or cell. So when you don't want it to be present, change cell pr header height.

It looks like the collection view is a table header view (the way you have positioned it in the storyboard).

Try setting it to nil if data is empty

self.tableView.tableHeaderView = nil

The RecommendedView is a tableHeaderView . Try to have a outlet to the height constraint of the RecommendedView.

Set the height constraint to 0 in case you don't have any element in the collection view.

@IBOutlet var tableHeaderHeighConstraint: NSLayoutConstraint!

override func viewDidLoad(){
    if collectionIsEmpty(){
        tableHeaderHeighConstraint.constant = 0
        self.tableView.tableHeaderView?.layoutIfNeeded()
    }
}

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