How do I create a grid of icons like the iPhone home screen?

前端 未结 6 1155
悲哀的现实
悲哀的现实 2021-01-30 11:58

How should I go about creating a UI similar to the Springboard (home screen) on the iPhone? I\'d like a grid of evenly spaced buttons with images where I can respond to the butt

6条回答
  •  感动是毒
    2021-01-30 12:43

    @August: I took your advice and added the UIButtons as subviews of the UIView rather than look further into the UITable. Thanks!

    Hopefully the code below will help to jumpstart someone else. I've statically placed 4 buttons in a grid. It shouldn't be much harder to place any number of buttons according to the parent UIView's bounds and orientation.

    @implementation IconView
    - (id)initWithFrame:(struct CGRect)windowRect{
      self = [super initWithFrame: windowRect];
      if (self != nil){
        for(int i = 0; i < 4; i++){
          buttons[i] = [self buildButton];
          [self addSubview: buttons[i]];
        }
        [self reInit];
      }
      return self;
    }
    - (UIButton *) buildButton{
      UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
      [button setBackgroundImage: [[UIImage imageNamed:@"icon.png"] stretchableImageWithLeftCapWidth:60 topCapHeight:0] forState:UIControlStateNormal];
      return [button autorelease];
    }
    - (void)reInit{
      CGRect rect;
      rect = buttons[0].frame;
      rect.origin = CGPointMake(5, 5);
      buttons[0].frame = rect;
    
      rect = buttons[1].frame;
      rect.origin = CGPointMake(70, 5);
      buttons[1].frame = rect;
    
      rect = buttons[2].frame;
      rect.origin = CGPointMake(5, 70);
      buttons[2].frame = rect;
    
      rect = buttons[3].frame;
      rect.origin = CGPointMake(70, 70);
      buttons[3].frame = rect;
    }
    @end
    

提交回复
热议问题