Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在IOS中向Uiscrollview反向添加动态元素?_Ios_Iphone_Ipad_Uiscrollview - Fatal编程技术网

如何在IOS中向Uiscrollview反向添加动态元素?

如何在IOS中向Uiscrollview反向添加动态元素?,ios,iphone,ipad,uiscrollview,Ios,Iphone,Ipad,Uiscrollview,我的应用程序中有一个scrollview,其中包含动态元素,我从右向左添加这些元素,如下所示 if(array.count>0) { [[cell.contentView viewWithTag:777] removeFromSuperview]; float x=315.0; float width=0.0; UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRe

我的应用程序中有一个scrollview,其中包含动态元素,我从右向左添加这些元素,如下所示

if(array.count>0)
    {
          [[cell.contentView viewWithTag:777] removeFromSuperview];

    float x=315.0;
    float width=0.0;
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(70, 52,245,20)];
    scroll.backgroundColor=[UIColor clearColor];
    scroll.tag=777;
    [scroll setContentOffset:CGPointMake(70,245)];
    scroll.showsHorizontalScrollIndicator=NO;

    for(int i =0; i <[array count] ; i++)
    {

        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

        [btn setBackgroundColor:[UIColor colorWithRed:133.0/255.0 green:155.0/255.0 blue:179.0/255.0 alpha:1.0]];
        btn.layer.cornerRadius = 2;


        [btn setTitle:[[array objectAtIndex:i] objectForKey:@“ggg”] forState:UIControlStateNormal];
        btn.userInteractionEnabled=NO;
        [btn.titleLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:10.0]];
       CGSize size = [btn.titleLabel.text sizeWithAttributes:
        @{NSFontAttributeName:
              [UIFont fontWithName:@"Helvetica Neue" size:10.0]}];
        x=x-(size.width+10);

        //or whatever font you're using
        [btn setFrame:CGRectMake(x,0,size.width+10,15)];
        [scroll addSubview:btn];
         width=width+x;
         x=x-5;
    }

       scroll.contentSize=CGSizeMake((width+50),20);
        // [scroll setPagingEnabled:YES];
    [cell.contentView addSubview:scroll];
}

这里一切都很好,但我无法滚动到左边添加的最后一个元素。滚动只允许我从左向右滚动到70。有人能告诉我这方面的错误吗

-(void)addButtonInScrollViewFromArray:(NSArray *)array{
NSInteger arrayCount=array.count;
NSInteger numberIteration=0;
CGFloat buttonXValue=300;
UIScrollView *buttonScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 360, 60)];
while (numberIteration<arrayCount) {
    UIButton *buttonCreated=[UIButton buttonWithType:UIButtonTypeCustom];
    buttonCreated.frame=CGRectMake(buttonXValue,5, 50, 50);
    buttonXValue-=60;
    [buttonCreated setTitle:[NSString stringWithFormat:@"Button%d",numberIteration] forState:UIControlStateNormal];
    [buttonScrollView addSubview:buttonCreated];
    numberIteration++;
}
CGFloat totalLength=numberIteration*60;
buttonScrollView.contentSize=CGSizeMake(totalLength,60);
}

此函数可以传递数组


你能试着在一点以下吗

scroll.contentSize = CGSizeMake(x+size.width+10,20);

试试这个卷轴。contentInset=UIEdgeInsetsZero@约翰库马尔:对不起,运气不好