Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:01:24+00:00 2026-06-06T05:01:24+00:00

I have used this custom grid view in my code, and I want to

  • 0

I have used this custom grid view in my code, and I want to create a grid view which looks like this:
enter image description here

How should I do this in the code? Here’s the setup method for items:

- (void)setupItemViews {
for (UIView *view in self.itemViews) {
    [view removeFromSuperview];
}
for (UIImageView *image in self.images) {
    [image removeFromSuperview];
}

[self.images removeAllObjects];
[self.itemViews removeAllObjects];

// now add the new objects
NSUInteger numItems = [self.menuDelegate menuViewNumberOfItems:self];

for (NSUInteger i = 0; i < numItems; i++) {
    GridMenuItemView *itemView = [[GridMenuItemView alloc] init];

    GridMenuItem *menuItem = [self.menuDelegate menuView:self itemForIndex:i];

    itemView.frame = CGRectMake(0, 0, self.itemSize.width, self.itemSize.height);
    itemView.label.text = menuItem.title;
    //itemView.imageView.image = menuItem.icon;
    itemView.tag = i;

    [itemView addTarget:self
                 action:@selector(itemPressed:) 
       forControlEvents:UIControlEventTouchUpInside];

    [itemView setBackgroundImage:[UIImage imageNamed:menuItem.normalImageName]
                        forState:UIControlStateNormal];
    [itemView setBackgroundImage:[UIImage imageNamed:menuItem.highlightedImageName]
                        forState:UIControlStateHighlighted];

    NSUInteger numColumns = self.bounds.size.width > self.bounds.size.height ? self.columnCountLandscape : self.columnCountPortrait;


    [self.itemViews addObject:itemView];
    [self addSubview:itemView];


}

Thanks to ctrahey, I got what I wanted. But the method used for setting up the cells is -(void) layoutSubviews as follows:

You can find the corresponding code after this line: **//__n__ ...
– (void)layoutSubviews {
[super layoutSubviews];
//[self removeFromSuperview];

NSUInteger numColumns = self.bounds.size.width > self.bounds.size.height ? self.columnCountLandscape : self.columnCountPortrait;

NSUInteger numItems = [self.menuDelegate menuViewNumberOfItems:self];
if (self.itemViews.count != numItems) {
    [self setupItemViews];
}

CGFloat padding = roundf((self.bounds.size.width - (self.itemSize.width * numColumns)) / (numColumns + 1));
NSUInteger numRows = numItems % numColumns == 0 ? (numItems / numColumns) : (numItems / numColumns) + 1;

CGFloat yPadding = 0;
CGFloat totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding ;

// get an even y padding if less than the max number of rows

if (totalHeight < self.bounds.size.height) {
    CGFloat leftoverHeight = self.bounds.size.height - totalHeight;
    CGFloat extraYPadding = roundf(leftoverHeight / (numRows + 1));
    yPadding += extraYPadding;

    totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding;
}

// get an even x padding if we have less than a single row of items
if (numRows == 1 && numItems < numColumns) {
    padding = roundf((self.bounds.size.width - (numItems * self.itemSize.width)) / (numItems + 1));
}

for (int i = 0, j = numColumns; i < numItems; i++) {

    j--;
    if (j<0) {
        j += numColumns;
    }
    //NSUInteger column = j ;
    //NSUInteger row = i / numColumns;
    UIView *item = [self.itemViews objectAtIndex:i];


    //CGFloat xOffset = (column * (self.itemSize.width + padding)) + padding;
    //CGFloat yOffset = (row * (self.itemSize.height + yPadding));// + yPadding;


    **//__n__ These three lines of code make set the cell items at different xPositions from right and left.  
    CGFloat xPosition = (i%2) ? 0.0 : self.bounds.size.width - self.itemSize.width;
    CGFloat yPosition = i*self.itemSize.height;
    item.frame = CGRectMake(xPosition, yPosition, self.itemSize.width, self.itemSize.height);**

    //item.frame = CGRectMake(xOffset, yOffset, self.itemSize.width, self.itemSize.height);

  //  if ((i%numColumns) == 0) {
     //   UIImageView *img = [self.images objectAtIndex:i/numColumns];
     //   img.frame = CGRectMake(0, yOffset+76, self.bounds.size.width, 1);


 //   }//End If
}
    //Scroll size.
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];


if (UIDeviceOrientationIsPortrait(currentOrientation))
{
    self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+1.55)));
}


else if (UIDeviceOrientationIsLandscape(currentOrientation))
{

    self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+3)));
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:01:25+00:00Added an answer on June 6, 2026 at 5:01 am

    If I understand your intention correctly, the line:

    itemView.frame = CGRectMake(0, 0, self.itemSize.width, self.itemSize.height);
    

    Should be using your index i and some simple arithmetic to position the views. One technique I have used in the past is a simple is-odd check using the modulus operator.

    CGFloat xPosition = (i%2) ? 0.0 : self.bounds.size.width - self.itemSize.width;
    CGFloat yPosition = i*self.itemSize.height;
    itemView.frame = CGRectMake(xPosition, yPosition, self.itemSize.width, self.itemSize.height);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this custom JSlider, which will be used in many other forms/windows. But
Hi all I have custom grid view with imageButton and textView as follows Here
I have used this code (kind of tutorial) at http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5 ... In this code,
I have used this code for extracting urls from web page.But in the line
I have used this page from MSDN to apply a custom style to my
I have the following cell which is used for my custom column data type
Is it possible to create a custom theme and have it used as the
I have a custom control that is derived from ListView . This list view
I have used this website for testing http://www.webpagetest.org and among some suggestions for optimization
i have used this: Margins margins = new Margins(5, 5, 5, 5); printForm1.PrinterSettings.DefaultPageSettings.Margins =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.