简体   繁体   English

UIScrollView水平分布UIButtons

[英]UIScrollView distribute UIButtons Horizontally

I have a UIScrollView that will expand depending on how many buttons it is due to display. 我有一个UIScrollView ,它会根据要显示的按钮数量而扩展。 It will always round up to the nearest 320 multiple. 它将始终四舍五入到最接近的320倍。

So, if the UIScrollView will receive 3 buttons it will be 320px horizontal, if it received 6 then 640px, 9 is 960. 因此,如果UIScrollView将收到3个按钮,则水平方向为320px,如果收到6个,则为640px,9为960。

What I need to do is distribute the UIButtons across this UIScrollView evenly.. However I can't seem to figure out how to do this. 我需要做的是将UIButtons均匀地分布在此UIScrollView 。但是,我似乎无法弄清楚该如何做。

My UIButtons measure 80 pixels horizontally. 我的UIButtons水平测量80像素。

Could somebody help me get my head round this? 有人可以帮我解决这个问题吗?

Update/Clarification: I have no problem setting the UIScrollView's width. 更新/说明:设置UIScrollView的宽度没有问题。 This has been done. 已经完成了。 I am looking for info on how to distribute the 'UIButtons' within the 'UIScrollView'.. 我正在寻找有关如何在“ UIScrollView”中分发“ UIButton”的信息。

[theScrollView setContentSize:CGSizeMake(320/640/960, theScrollView.frame.size.height)];
   //considering a margin of 10 pts on either edges and a additional 10 pts on each side of button 

Add the buttons like below: 添加如下所示的按钮:

for(int i=0; i< buttonCount;i++){
    CGRect buttonFrame = CGRectMake( 10 + i*(80+10+10) +10 , buttonY, 80, buttonHeight );
    UIButton *button = [UIButton buttonWithType:<type>];
    button.frame = buttonFrame;
}

//To set the scrollable area

[scrollView setContentSize:CGSizeMake( 10 + buttonCount*(80+10+10) +10, 0 )];
//left margin +  buttonCount x (buttonWidth + leftButtonMargin+rightButtonMargin) + right margin

This should give you an almost evenly distributed buttons according to the number of buttons. 根据按钮的数量,这应该为您提供几乎均匀分布的按钮。

You can use like this:Use MyScrollView.pagingEnabled=YES. 您可以这样使用:使用MyScrollView.pagingEnabled = YES。

int PageNum=0;

if(TotalButtons%3 >0)
{
  pageNum=TotalButtons/3;
  pageNum++; 
}
else{
PageNum=TotalButtons/3;
}

[MyScrollView setContentSize:CGSizeMake(320*pageNum, MyScrollView.frame.size.height)];

Add Buttons according to the width and spacing you want on the scrollview. 根据所需的宽度和间距在滚动视图上添加按钮。

Edit: 编辑:

 int x=56;
    int y=60;

    for(int i=1;i<= 60;i++)
    {
        UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame=CGRectMake(x, y, 200, 120);
        button.tag=i;
        // [button setTitle:[NSString stringWithFormat:@"Video-%d",i+1] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"220x200.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [VideoStoreScroll addSubview:button];

        if(i%6==0)
        {
            count++;

            x=(count-1)*VideoStoreScroll.frame.size.width+56;

            y=50;
        }
        else
        {
            if(i%3==0)
            {
                x=(count-1)*VideoStoreScroll.frame.size.width+56;
                y=y+190;
            }
            else
            {
                x=x+256;
            }
        }
    }

    VideoStoreScroll.contentSize=CGSizeMake(x-56, 480);
    VideoStoreScroll.contentOffset=CGPointMake(0, 0);

Here I am using six buttons horizontally on scroll per page.You can work for three following this. 在这里,我每页滚动水平使用六个按钮。此后您可以使用三个按钮。

First take all the buttons in an array..lets say buttonsArray 首先将所有按钮放在一个buttonsArray

int spacing = 10;       // Adjust according to you requirement
int buttonWidth = 100;  // Adjust according to you requirement
int buttonHeight = 100; // Adjust according to you requirement
int count = 0;

for (UIButton *thisButton in buttonsArray) {
    [thisButton setFrame:CGRectMake(spacing*(count+1)+count*buttonWidth, 20, buttonWidth, buttonHeight)];
    count++;
}

[theScrollView setContentSize:CGSizeMake([[buttonsArray lastObject]frame].origin.x+[[buttonsArray lastObject]frame].size.width+spacing, 0)];

Hope this will help you. 希望这会帮助你。 I have tested it..working fine... 我已经测试了..工作正常...

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

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