开发者

Scroll View Not Functioning Properly

I am using following code: slidingImageIconView=[[UIScrollView alloc]init];

[slidingImageIconScrollView setContentSize:CGSizeMake(110, 90)];

UIButton *iconImageSlide=[[UIButton alloc] init];
[iconImageSlide setImage:[UIImage imageNamed:@"dummy_img.jpg"] forState:UIControlStateNormal];
[slidingImageIconScrollView addSubview:iconImageSlide];

but my button is not adding in scroll v开发者_如何学编程iew, i know i am missing something very silly but please help me..


I notice that you have slidingImageIconView and slidingImageIconScrollView. Is that a typo? I've left it like that in my examples here.

You need to initWithFrame in order to set the button position and size:

slidingImageIconView=[[UIScrollView alloc]init];

[slidingImageIconScrollView setContentSize:CGSizeMake(110, 90)];

UIButton *iconImageSlide=[[UIButton alloc] initWithFrame:CGRectMake(0,0,100,20)];
[iconImageSlide setImage:[UIImage imageNamed:@"dummy_img.jpg"] forState:UIControlStateNormal];
[slidingImageIconScrollView addSubview:iconImageSlide];

If you want to make the button the same size as the image - then the following rearrangement would allow that. Also note that I've used setBackgroundImage instead of setImage. I tend to use setImage to put an 'icon' on the button, but setBackgroundImage for the actual button-image.

UIImage* buttonImage = [UIImage imageNamed:@"dummy_img.jpg"];

slidingImageIconView=[[UIScrollView alloc]init];

[slidingImageIconScrollView setContentSize:CGSizeMake(110, 90)];

UIButton *iconImageSlide=[[UIButton alloc] initWithFrame:CGRectMake(0,0,buttonImage.size.width,buttonImage.size.height)];
[iconImageSlide setBackgroundImage:buttonImage forState:UIControlStateNormal];
[slidingImageIconScrollView addSubview:iconImageSlide];


You haven't set the frame of the buttonf it has no useful geometry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜