繁体   English   中英

如何在iOS中根据Superview的大小制作UIButton网格?

[英]How to make a UIButton grid according to size of superview in iOS?

我正在尝试根据superView的大小制作一个UIButton Grid。我见过的大多数解决方案都是在向scrollView添加按钮的地方,但是如果我不想要可滚动的网格而只是一个普通的网格怎么办?我会根据其绘制视图的大小来调整按钮的大小吗?有人尝试过吗?真的很感谢您的帮助。

-(void)createLayout{

int width =30;
int height =30;


int leftMargin =10;
int topMargin =10;

int xTile;
int yTile;

int xSpacing = 50;
int ySpacing = 50;


    for (int c=1; c<=5; c++) {
        for (int r=1; r<=10; r++) {
        NSLog(@"row : %d  col : %d",r,c);

        yTile = ((c*ySpacing)+topMargin);
        xTile = ((r*xSpacing)+leftMargin);

        Test *btn = [Test buttonWithType:UIButtonTypeCustom];
        btn.row = r;
        btn.column =c;
        [btn setTitle:[NSString stringWithFormat:@"%d,%d",btn.row,btn.column] forState:UIControlStateNormal ];
        [btn.titleLabel setFont:[UIFont fontWithName:@"Arial" size:14]];
        [btn setBackgroundColor:[UIColor redColor]];
        [btn setFrame:CGRectMake(xTile, yTile, width, height)];
        [self.scrollView  addSubview:btn];
        xTile=xTile+10;

    }
     yTile = yTile+10;

    }

    }

很好回答我写的代码。 这样的事情会帮助您。 我已经在这里为您创建了这个项目 检查一下。 同样,我希望您继续努力并在完成时做出承诺,这还不是很好。

- (UIView *) view : (CGSize) viewSize withButtons : (int) numberOfButtons{
    CGRect frame = CGRectZero;
    frame.size = viewSize;
    UIView *view = [[UIView alloc]initWithFrame:frame];//considering View size is after margine

    CGSize buttonSize = [self getButton:numberOfButtons sizeFor:viewSize];

    for (int i = 1; i <= numberOfButtons; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:[self getButton:buttonSize frameForIndex:i inView:viewSize]];
        [button setTitle:[[NSNumber numberWithInt:i] stringValue] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [button.layer setBorderColor:[UIColor lightGrayColor].CGColor];
        [button.layer setBorderWidth:1.0f];
        [view addSubview:button];
    }
    return view;
}

- (CGRect) getButton : (CGSize) buttonSize frameForIndex : (int) buttonIndex inView : (CGSize) containerSize{
    int x = buttonIndex % (int)(containerSize.width / buttonSize.width);
    x *= buttonSize.width;
    int y = buttonIndex % (int)(containerSize.height / buttonSize.height);
    y *= buttonSize.height;

    CGRect frame = CGRectZero;
    frame.origin = CGPointMake(x, y);
    frame.size = buttonSize;

    return frame;
}

- (CGSize) getButton : (int) numberOfButtons sizeFor : (CGSize) containerSize{
    double containerArea = containerSize.width * containerSize.height;
    double areaOfOneButton = containerArea / numberOfButtons;
    double edgeOfOneButton = sqrt(areaOfOneButton);
    edgeOfOneButton = (edgeOfOneButton > containerSize.width) ? containerSize.width : edgeOfOneButton;

    return CGSizeMake(edgeOfOneButton, edgeOfOneButton);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM