Search code examples
iosuitableviewimessage-extension

iMessage Extension - need to know the height of the iMessages header


I'm working on a "Cats" iMessage Extension. (Ok, I'm not, but I'm required by the client to sanitize my SO questions.) Here's what I'm seeing in my tableView:

enter image description here

As you can see, my table's section headers have a large gap under my search bar. I can easily fix this with this code.

    self.catsTableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)

Well, almost easily, that 20 gives me great concern. You see, that 20 is different on different devices. This is unacceptable, I can't hardcode a device table.

My first guess was to try and adjust the top guide, but that's always zero, or a constraint, but I can't find one to adjust!

Q: Why is the gap there?

A: The gap is there because this is an iMessage Extension, the size of gap is exactly the size of the "Kate" header above. You can see this in these two screen shots. First, an iPhone7 showing a 168px gap that is exactly equal to the 168px header.:

enter image description here

And next in iPhone SE, showing a 126px gap, exactly matching the 126px header:

enter image description here

What's a good, device independent, way to solve this?


Solution

  • I found the answer in this question. Here's the code that worked for me.

    override func viewDidLayoutSubviews() {
        self.recentQuotesTableView.contentInset = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, 0, 0)
    }
    

    It works on all devices, including iPad with split view.