maxsokolov / TableKit

Type-safe declarative table views.
MIT License
706 stars 74 forks source link

Missing header view using AutoLayout #70

Closed danmartyn closed 6 years ago

danmartyn commented 6 years ago

I've created a UITableViewHeaderFooterView subclass, with just a simple label for now to test things out. I've pinned the label to the edges using constraints (all in code). Then, when creating my sections in my view controller, I create a new header view, configure it with the data it needs to set some text in the label and use that as the header view in the section initializer. However, when I run the app, the header doesn't show up. If I add break points, I can see that the view is created and the correct text is set, it just isn't visible... Do I need to manually specify the height of the header? It can't use AutoLayout to figure that out for me?

maxsokolov commented 6 years ago

Hi @danmartyn

UITableViewHeaderFooterView is not supported by TableKit, so you have to use UIView.

I think you are able to use auto layout for your header or footer view. You just need to calculate right height like this (see apple docs):

func getViewHeight(view: UIView, width: CGFloat) -> CGFloat {

    view.frame = CGRect(x: 0, y: 0, width: width, height: view.frame.size.height)
    view.setNeedsLayout()
    view.layoutIfNeeded()

    return view.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
}

and then set it to your section:

let view = MyAutolayoutView()
let section = TableSection(headerView: view, footerView: nil)
section.headerHeight = getViewHeight(view: view, width: screenWidth)

I hope it helps you.

danmartyn commented 6 years ago

Using a view doesn't seem to be working. It doesn't show up? I can see the space at the top left for it, and again, with breakpoints I can see the view is created and configured, I just don't see it in the table...

danmartyn commented 6 years ago

I just did a test. I created a test view, of the kind I want to use as my section header and added it to the view, above the table and centered it. That works, I can see the view (and its subviews) above the table. If I use just a normal UIView with a background color set as the section header, that works as well. I can see the color in the table. But if I try to use the custom view I want to use, it doesn't work. And this is with the auto layout constraints and just setting a hardcoded height as well. Thoughts?

danmartyn commented 6 years ago

Ok, I think I've narrowed this one down to one of my own extensions for setting up views in a closure. When I call my configure method in the closure, it doesn't work. If I call my configure method outside the closure, it works. ¯_(ツ)_/¯

maxsokolov commented 6 years ago

@danmartyn I've added an example to the demo with AutolayoutSectionHeaderView

maxsokolov commented 6 years ago

@danmartyn check out Autolayout cells section in the demo.