Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a a very simplistic implementation of a GridView. You'll likely need to customize it to your needs, but may be a good start. Just create it by called initWithFrame: and add your UIView objects to the children array. You may need to call setNeedsLayout after rotating, cannot remember.</p> <pre><code>// // GridView.h // Project // // Created by Anthony Picciano on 7/28/10. // #import &lt;UIKit/UIKit.h&gt; #define GRID_VIEW_DEFAULT_COLUMNS 1 #define GRID_VIEW_DEFAULT_HGAP 10.0f #define GRID_VIEW_DEFAULT_VGAP 10.0f #define GRID_VIEW_LAYOUT_NOTIFICATION @"layoutNotification" @interface GridView : UIView { int columns; float hgap; float vgap; } @property (nonatomic) int columns; @property (nonatomic) float hgap; @property (nonatomic) float vgap; @end // // GridView.m // Project // // Created by Anthony Picciano on 7/28/10. // #import "GridView.h" @implementation GridView @synthesize columns, hgap, vgap; - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { columns = GRID_VIEW_DEFAULT_COLUMNS; hgap = GRID_VIEW_DEFAULT_HGAP; vgap = GRID_VIEW_DEFAULT_VGAP; } return self; } - (void)layoutSubviews { float xpos = 0; float ypos = 0; float width = self.frame.size.width; float cellWidth = (width - (hgap * (columns - 1))) / columns; int currentColumn = 1; // columns number start at 1, not 0 float maxRowHeight = 0.0f; for (UIView *child in self.subviews) { CGRect childFrame = CGRectMake(xpos, ypos, cellWidth, child.frame.size.height); child.frame = childFrame; if (child.frame.size.height &gt; maxRowHeight) { maxRowHeight = child.frame.size.height; } if (currentColumn &lt; columns) { currentColumn++; xpos += cellWidth + hgap; } else { currentColumn = 1; xpos = 0.0f; ypos += maxRowHeight + vgap; maxRowHeight = 0.0f; } } if (currentColumn == 1) { ypos -= vgap; } else { ypos += maxRowHeight; } CGRect aFrame = self.frame; aFrame.size = CGSizeMake(width, ypos); self.frame = aFrame; NSNotification *notification = [NSNotification notificationWithName:GRID_VIEW_LAYOUT_NOTIFICATION object:nil]; [[NSNotificationCenter defaultCenter] postNotification:notification]; } - (void)dealloc { [super dealloc]; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload