Use Adaptive Layout programmatically to change the height of static cells for hCompact

In a portable word game only, I use static cells to display IAP storage:

iPhone screenshot

You can see my problem in the screenshot of the iPhone 4 above - the pink button (for viewing video ads and receiving 150 coins) is not visible below.

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

Xcode screenshot

I use 7 static cells:

  • Blue upper cell with back button, name, money bag icon
  • Status text (not displayed in the screenshot)
  • Coin Pack 1
  • Coin collection 2
  • Coin collection 3
  • Coin Pack 4
  • Video ads (the pink cell below is a problem that is not visible on iPhone 4 and other compact devices).

And resize the cells using 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?
}

: ( hCompact Adaptive Layout).

UPDATE:

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;
}
+4
3

,

- (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];
}
+1

UIViewController traitCollection, .

:

if self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact {
    return 70;
}
else {
    return 90
}
+1

As a workaround in this case, anyway with so few cells, scrollview with subviews containers is perfect for your needs. Some more code that I know, but this is no worse than manually calculating the height of each cell. Waiting for a better solution.

0
source

All Articles