PaoloCuscela / Cards

Awesome iOS 11 appstore cards in swift 5.
MIT License
4.2k stars 274 forks source link

UITableView in DetailView #52

Closed zaccadams closed 5 years ago

zaccadams commented 6 years ago

A Reopen of case #31 Hi, I've tried to implement a UITableView inside the DetailView but as mention in #31 the scroll acts up and it doesn't function the way it should.

I tried disabling the scroll on the table view and yes that does make the scroll work properly but then when I scroll to the bottom it's cutting off the tableview as in it's not resizing to the height of the tableview.

I want to achieve something like: 34078976-3ca3549c-e367-11e7-9b7e-79a522ec35e8

This is what the result is:

iph_git_cards

The table view should show all the way until Row:2 Section:5 but it is cut off.

Any help would be much appreciated. Thanks in advanced.

PaoloCuscela commented 6 years ago

Did you managed to solve this ? If not could you share an example project ?

mteera commented 6 years ago

I asked a very similar question and gave you a sample project. Could you please have a look at it.

PaoloCuscela commented 6 years ago

@mteera I can't find it. Could you please link it ? @zacadams01 Did you have the same issue even with the 1.3.3 version ?

mteera commented 6 years ago

@PaoloCuscela did you look at https://github.com/PaoloCuscela/Cards/issues/41 ?

zaccadams commented 6 years ago

Sorry I thought I responded to this case. I solved my problem, probably not in the most efficient or best way. But it works and it works for me. Hopefully with this a proper fix can come about.

Firstly I set an outlet for the fixed height constraint for the tableView on the CardViewController (The view controller shown when the card is open)

Secondly I made this function that gets the content size of the tableview (All heights of cells, headers, footers added up) then set the tableView height constraint to equal the content size, all within the CardViewController. I only call this once the table view has been reloaded and all data loaded:

 func resetViewAndPost() {

    if UIScreen.main.nativeBounds.height == 2436 { //only checking for portrait bounds
        // IS IPHONE X add extra bounds for bottom
        self.tableViewHeightConstraint.constant = self.tableView.contentSize.height+30
    } else {
        self.tableViewHeightConstraint.constant = self.tableView.contentSize.height
    }

    // Set the views height to the content size of the table view
    self.view.frame.size.height = self.tableView.contentSize.height

    self.viewDidLayoutSubviews()
    self.tableView.layoutIfNeeded()
    self.tableView.updateConstraints()

    // Post a flag
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "resetDetailView"), object: nil)
}

Add Notification Receiver in the viewDidLoad of DetailViewController in the Cards files NotificationCenter.default.addObserver(self, selector: #selector(self.resetDetailViewHeight(notification:)), name: NSNotification.Name(rawValue: "resetDetailView"), object: nil) //Requires Reset Of View

Then I add this function to the DetailViewController that is called when Post message is received which resets the frame to the new height of the CardViewController

 @objc func resetDetailViewHeight(notification: NSNotification) {
    if let detail = detailView {

        detail.alpha = 1
        if UIScreen.main.nativeBounds.height == 2436 {
            // IS IPHONE X add extra bounds
            detail.frame = CGRect(x: 0,
                              y: card.backgroundIV.bounds.maxY,
                              width: scrollView.frame.width,
                              height: detail.frame.height+30)
        } else {
            detail.frame = CGRect(x: 0,
                                  y: card.backgroundIV.bounds.maxY,
                                  width: scrollView.frame.width,
                                  height: detail.frame.height)
        }

        scrollView.contentSize = CGSize(width: scrollView.bounds.width, height: detail.frame.maxY)

    }
}

So now once you have finished getting your data and reloading the tableview you call resetViewAndPost() and it will set the constraints and reset the card view bounds.

I hope this makes sense, I would make an example project but I really don't have that much time with Work, Study on the Side Stuff. But I'll try and make one if these instructions aren't that clear :)

This doesn't enable the tableview to be seen from the inital card before it is presented.

zaccadams commented 6 years ago

@PaoloCuscela I had this problem on what ever version was the latest when I made this ticket. I'm not sure if it is still a problem in any newer versions, if there has been, but this method hasn't let me down.

zaccadams commented 6 years ago

I also disabled the scrollView on the tableView in CardViewController self.tableView.isScrollEnabled = false

PaoloCuscela commented 6 years ago

I managed to solve this by changing in DetailViewController -> viewDidLoad()

if let detail = detailView {

            scrollView.addSubview(detail)
            detail.alpha = 0
            detail.autoresizingMask = .flexibleHeight //previously .flexibleWidth
        }

Can you do some testing and see if this solves your issues ?

mteera commented 6 years ago

Sorry you'll have to explain a bit clearer than that. What do I do inside DetailViewController?

cylak commented 6 years ago

Actually it's work better with this:

detail.autoresizingMask = [.flexibleBottomMargin, .flexibleHeight]

Tested in UICollectionView and works!

Theysn commented 5 years ago

I got the same issue,
first of all you need to make your tableView unscrollable self.tableView.isScrollEnabled = false

second you need to determine the content height of your tableView, I declared a variable called tableViewHeight to get this value :

   var tableViewHeight: CGFloat {
    tableView.layoutIfNeeded()

    return tableView.contentSize.height
  }

After that you need to set the height of the entire view to the height of your tableview after reloading data.

self.view.frame.size.height = self.tableViewHeight