在水平滚动中添加图像不显示图像

时间:2014-04-07 07:50:13

标签: ios objective-c ios7

我在滚动视图中添加了一些图像我得到了舞会图片没有显示在滚动视图上所以请帮忙,我的代码在下面。

-(void)addscroll
{
    CGFloat btnX = 160.0;
    int numberOfButton = 10;
    for (UIView *subview in scrollView.subviews) {
        [subview removeFromSuperview];
    }

    for (int i = 1 ; i <= numberOfButton; i++)
    {
        UIImageView *img=[[UIImageView alloc]init];
        img.image=[UIImage imageNamed:@"1.png"];
        //img.tag = i;
        //[button setTitle:@"Excavations" forState:UIControlStateNormal];
        img.frame = CGRectMake(160.0, 200.0, 160.0, 40.0);
        [scrollView addSubview:img];
        btnX = btnX + 165.0;
    }
    scrollView.contentSize = CGSizeMake(btnX + 50, 150);
}

6 个答案:

答案 0 :(得分:0)

您正在计算错误的图像视图框

    img.frame = CGRectMake(btnX, 200.0, 160.0, 40.0);

答案 1 :(得分:0)

-(void)addscroll
{
    CGFloat btnX = 160.0;
    int numberOfButton = 10;
    for (UIView *subview in scrollView.subviews) {
        [subview removeFromSuperview];
    }

    for (int i = 0 ; i < numberOfButton; i++)
    {
        UIImageView *img=[[UIImageView alloc]init];
        img.image=[UIImage imageNamed:@"1.png"];
        //img.tag = i;
        //[button setTitle:@"Excavations" forState:UIControlStateNormal];
        img.frame = CGRectMake(i*btnX +10, 200.0, 160.0, 40.0);
        [scrollView addSubview:img];
    }
    scrollView.contentSize = CGSizeMake((btnX + 10)*numberOfButton, 160);
}

答案 2 :(得分:0)

您的滚动视图的高度为150,您的按钮y值为200,表示超出可见帧。

答案 3 :(得分:0)

试试这个,

-(void)addscroll
{
    CGFloat btnX = 160.0;
    int numberOfButton = 10;
    for (UIView *subview in scrollView.subviews) {
        [subview removeFromSuperview];
    }

    for (int i = 1 ; i <= numberOfButton; i++)
    {
        UIImageView *img=[[UIImageView alloc]init];
        img.image=[UIImage imageNamed:@"1.png"];
        img.frame = CGRectMake(btnX, 105, 160.0, 40.0);
        [scrollView addSubview:img];
        btnX = btnX + 165.0;
    }
    scrollView.contentSize = CGSizeMake(btnX + 50, 150);
}

您将img框架的x设置为160.0,因此所有img都会添加到同一位置,将其更改为btnX并更新btnX在每次迭代。接下来是contentSize scrollView,其内容高度设置为150,img添加到y的200.因此,无法看到超出scrollview高度的内容。

答案 4 :(得分:0)

使用此代码。还要确保img.image不是nil并且存在“1.png”。您将滚动限制为小于200而图像从200开始

- (无效)addscroll

{

CGFloat btnX = 0;
int numberOfButton = 10;
for (UIView *subview in scrollView.subviews) {
    [subview removeFromSuperview];
}

for (int i = 1 ; i <= numberOfButton; i++)
{
    UIImageView *img=[[UIImageView alloc]init];
    img.image=[UIImage imageNamed:@"1.png"];
    img.frame = CGRectMake(btnX, 200.0, 160.0, 40.0);
    [scrollView addSubview:img];
    btnX = btnX + 165.0;
}
scrollView.contentSize = CGSizeMake(btnX + 50, 250);}

答案 5 :(得分:0)

设置img.frame = CGRectMake(160.0, 0, 160.0, 40.0);

通过设置y = 200,您将使其超出可见父级的框架。滚动的最大高度为198,您的图像的y原点应该在此之前,以便使子视图可见。