Skip to content

Instantly share code, notes, and snippets.

@DwightChan
Forked from bobmoff/UIView+Stacker.h
Created October 14, 2016 12:33
Show Gist options
  • Select an option

  • Save DwightChan/fc27f3ca1a7c9170467859a4115c12f5 to your computer and use it in GitHub Desktop.

Select an option

Save DwightChan/fc27f3ca1a7c9170467859a4115c12f5 to your computer and use it in GitHub Desktop.

Revisions

  1. Fille created this gist Jul 10, 2013.
    7 changes: 7 additions & 0 deletions UIView+Stacker.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #import <UIKit/UIKit.h>

    @interface UIView (Stacker)

    - (void)stackSubviews;

    @end
    32 changes: 32 additions & 0 deletions UIView+Stacker.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #import "UIView+Stacker.h"

    @implementation UIView (Stacker)

    - (void)stackSubviews {

    // Init Y position of the first visible subview contained inside the StackerView
    float _y = 0;

    for (UIView *subview in self.subviews) {
    // Start by adjusting the Y position of the current subview
    subview.y = _y;

    // If the subview is visible, then add height to the global Y position, which is
    // used to set the Y position of the next subview in the iteration
    if (!subview.hidden) {

    // If the subview is a TableView, we use the contentsize instead of the frames height
    if ([subview isMemberOfClass:[UITableView class]]) {
    UITableView *tview = (UITableView *)subview;
    _y += tview.contentSize.height;
    } else {
    _y += subview.frame.size.height;
    }

    // Increase the height of acctual StackerView with each visible sub-view
    self.height = _y;
    }
    }
    }

    @end