1

I am working on a Swift iOS8/9 project and using Xcode 7, with storyboard / autolayout, etc. (For completeness: I started at the beginning on the year on older versions of Xcode, etc.)

I have a a UINavigationController hierarchy and some View Controllers contain a single UIView which contain a single child, a UITableView. (I am choosing not to use UITableViewController, so suffice to say ... it is not an option.)

The problem: The tableview starts 64 pixels (status plus nav height) too low:

Unwelcomed Gap to first Table Section Header

Now: I've had things working for some of my UIViewControllers containing a UICollectionView within a UIView but I achieved it (after lots of poking / prodding, and a while back) by a top alignment constraint from the top of the UICollectionView to the top of the Top Layout Guide. (Looking through my storyboard I some alignments and some vertical distances, but all to top of Top Layout Guide.)

I am looking for help understanding if I can do what I want, if the approach of constraining the top of the tableview to the top of the layout guide is correct, or if there is a better way.

Success through alignment of Top to Top

Unfortunately, I cannot seem to repeat this (perhaps 'cos I've forgotten some key press combination jujitsu, or new Xcode, or collection != table, or something in my view hierarchy. (I've checked all segues are adaptive push, I've even deleted/re-built this VC since I had one model segue, just in case it was confused that this VC isn't in a navigation view controller hierarchy.)

If I control/drag to connect or select the two to align:

Table View Inside View - Selecting TableView and Top Guide

this is what I am offered - note no "align to top":

enter image description here

Note: Here are the table settings that (to my understanding) should be adjusting edge inset and scroll offsets correctly, and do appear to ('cos it is the only child of the top view, per documentation.)

enter image description here

Here are the frames after view did layout subviews:

enter image description here

Adam Jack
  • 590
  • 5
  • 17

3 Answers3

2

The approach that I ended up using came to me when I spoke to @ProgrammingBonn and mentioned the frames. I saw that the primary UIView was position as I wanted, and so it (finally) dawned on me to align the top of UITableView to the top of the UIView.

(I suspect that much as I was building my own ViewController and not relying upon UITableViewController I had an over inflated view of this particular view within the hierarchy. It was a subview not a primary view, despite occupying all the screen real-estate.)

So, to be clear:

I selected the UITableView, then command/selected the UIView (the main one, and it's parent) and then used the "align" tool to top align those two.

enter image description here

Adam Jack
  • 590
  • 5
  • 17
1

Try unchecking the "Adjust Scroll View Insets" option.

petertoth
  • 249
  • 2
  • 6
  • That removes the gap, but doesn't achieve what I'd like to achieve (and apologies if I left it unstated, I can update if needs be) ... but it costs me the content sliding under the translucent navigation bar, it is as if I have an opaque navigation bar. – Adam Jack Oct 20 '15 at 22:03
0

I am not sure whether this will fix your problem but have you tried playing around with viewController.edgesForExtendedLayout?

See more info here: Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout in iOS7

Community
  • 1
  • 1
JWKot
  • 360
  • 3
  • 11
  • My understanding (and giving specifics not document speak) is that "adjusting the scroll view" gives a 64 pixel offset (so content is moved down to be initially visible) which is working/right (albeit from a UITableView that is 64 pixels too far down.) I believe "extending edges" (set to UIRectEdgeAll, I checked) moves the top UIView up 64 pixels (which is working, see frames.) I feel the problem is the UITableView is incorrectly positioned within the top UIView because it is tied to the bottom of the Top Layout Guide. – Adam Jack Oct 20 '15 at 22:48
  • Huh, thanks @ProgrammingBonn answering your comment led me to a solution. I'm not sure it is correct but I'll leave it here a while and answer this question if a more authoritative answer doesn't come along. As I said, the UIView top frame was correct, so instead of control drag to link the UITableView to it's container (getting me to the Top Layout Guide) I select the UITableView and the UIView and aligned their tops. – Adam Jack Oct 20 '15 at 22:54