4

In a portait-only word game I use static cells to display an IAP store:

iPhone screenshot

You can see my problem in the iPhone 4 screenshot above - the pink button (to watch video ads and receive 150 coins) at the bottom is not visible.

Here is my Xcode screenshot (please click for fullscreen):

Xcode screenshot

I use 7 static cells:

  • Blueish top cell with Back button, title, money bag icon
  • Status text (not visible in the above screenshot)
  • Coins pack 1
  • Coins pack 2
  • Coins pack 3
  • Coins pack 4
  • Video ads (pink cell at the bottom - has the problem of being not visible on iPhone 4 and other compact devices)

And resize the cells with this method:

- (CGFloat)tableView:(UITableView *)tableView
   heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0)
        return 70;

    if ([indexPath row] == 1)
        return 35;

    return 90; // XXX how to change this to 70 for hCompact?
}

My question is: how to resize the cells height programmatically for devices with compact height (the hCompact size class in Adaptive Layout).

UPDATE:

My own ugly solution has been sofar:

@interface StoreCoinsViewController ()
{
    int _cellHeight;
}

- (int)setCellHeight  // called in viewDidLoad
{
    int screenHeight = UIScreen.mainScreen.bounds.size.height;
    NSLog(@"screenHeight=%d", screenHeight);

    if (screenHeight >= 1024)  // iPad
        return 160;

    if (screenHeight >= 736)   // iPhone 6 Plus
        return 110;

    if (screenHeight >= 667)   // iPhone 6
        return 100;

    if (screenHeight >= 568)   // iPhone 5
        return 90;

    return 72;  // iPhone 4s (height=480) and earlier
}
- (CGFloat)tableView:(UITableView *)tableView
      heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0)
        return 70;

    if ([indexPath row] == 1)
        return 35;

    return _cellHeight;
}
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • 1
    If the cells are known can't you make it quick and dirty and get the table view height subtract the sum of previous cells height and return it for the last cell? – jalone Apr 20 '15 at 10:35
  • Then (as you can see at the iPhone 4 screenshot) I would only have few pixels left for the last cell. I would prefer to use smaller height for **all** cells - on hCompact devices. – Alexander Farber Apr 20 '15 at 10:59
  • 1
    Oh ok, i see, i am afraid that maybe with ios8 and [Auto sizing cells](https://www.captechconsulting.com/blogs/ios-8-tutorial-series-auto-sizing-table-cells) you may find a (dirty) solution, but for older OS, you have to do your math 'manually'. I am really interested tough. – jalone Apr 20 '15 at 11:05

3 Answers3

1

I would write a helper that looks at the vertical size Class for the current trait collection

- (CGFloat)verticalSizeForCurrentTraitCollection {
    switch (self.traitCollection.verticalSizeClass) {
        case UIUserInterfaceSizeClassCompact:
            return 70;
        case UIUserInterfaceSizeClassRegular:
            return 90;
        case UIUserInterfaceSizeClassUnspecified:
            return 80;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] == 0)
        return 70;

    if ([indexPath row] == 1)
        return 35;

    return [self verticalSizeForCurrentTraitCollection];
}
aahrens
  • 5,522
  • 7
  • 39
  • 63
  • Thank you. For some reason I get `UIUserInterfaceSizeClassRegular` for iPhone 4s, iPhone 6, iPad 2, iPad Retina (all - simulators in portrait modes) - which makes this approach unusable for my app... – Alexander Farber Apr 21 '15 at 19:32
1

Every UIViewController has a traitCollection property that you can use in code.

In your case you can check like this:

if self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact {
    return 70;
}
else {
    return 90
}
pteofil
  • 4,133
  • 17
  • 27
0

As a workaround in this case anyway with so little number of cells a scrollview with subviews containers would fit your needs perfectly. A little more code i know but it's no worst than to compute the height of each cell manually. Waiting for a better solution.

jalone
  • 1,953
  • 4
  • 27
  • 46