2014-04-07 98 views
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.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); 
} 
+0

你的scrollview的大小是多少? – NightFury

+0

(160,198,320,150) – user1672134

回答

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); 
} 

您設置img160.0,因此所有img將被添加到同一位置的X,將其更改爲btnX,並在每次迭代更新btnX。接下來是contentSizescrollView,它的內容高度設置爲150,並且img被添加到y的200.因此超出scrollview高度的內容不可見。

+0

現在感謝您的工作 – user1672134

+0

@ user1672134歡迎您。快樂編碼.. – Akhilrajtr

0

您計算圖像視圖的錯誤幀

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

仍然不能正常工作 – user1672134

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); 
} 
0

你的滾動視圖的高度是150,和你的按鈕的y值是200,這意味着是出於可見的框架。

+0

仍然不能正常工作 – user1672134

0

使用此代碼。還要確保img.image不是零,並且存在「1.png」。你只有有限的滾動不到200,而200在Y圖像開始

- (無效)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);} 
0

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

通過設置Y = 200,你正在使其脫離可見的父母的框架。滾動條的最大高度爲198,爲了使子視圖可見,圖像的y原點應該在此之前。