xcode 滚动视图中的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8260657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image In Scroll View
提问by Ahan Malhotra
I am using the following code to try and display an image in a UIScrollView.
我正在使用以下代码尝试在 UIScrollView 中显示图像。
[self.jarView addSubview:imgView];
It does not work. However, it works when I add the subview to my normal view. My UIScrollView is called jarView and imgView is a UIImageView.
这是行不通的。但是,当我将子视图添加到我的普通视图时,它会起作用。我的 UIScrollView 被称为 jarView 和 imgView 是一个 UIImageView。
Thanks
谢谢
采纳答案by GauravBoss
This works fine for me:
这对我来说很好用:
UIScrollView *scrollview;
scrollview= [[UIScrollView alloc] initWithFrame:CGRectMake(,,,)];// set scroll view size
view = [[UIView alloc] initWithFrame:CGRectMake(y1,x1,y2,x2)]; //set the view size
UIImage *image1 = [UIImage imageNamed:imageName];
image=[[UIImageView alloc]initWithImage:image1];// take image size according to view
[view addSubview:image];
[scrollview addSubview:view];
[view release];
At last define how much scrollview
is scroll:
最后定义scrollview
滚动量:
scrollview.contentSize = CGSizeMake(width,height);
回答by Igor Kulagin
Make sure that your scrollView.contentSize
property value is set correctly (width and height should be > 0).
确保您的scrollView.contentSize
属性值设置正确(宽度和高度应 > 0)。
回答by GauravBoss
I am giving you my sample code. In this code i add multiple images on scroll view and this work fine for me:
我给你我的示例代码。在这段代码中,我在滚动视图上添加了多个图像,这对我来说很好用:
UIScrollView *scrollview;
- (void)anyFunction
{
scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, 50)];
NSInteger viewcount = [cat_Products count]; // if you have four content your view count is 4..(four)..in my case i fetch it from database
CGFloat y2 = 0;
UIView *view;
UIImageView *imageView;
NSString *imageName;
for(int i = 0; i< viewcount; i++)
{
CGFloat y = i * 64;
y2 = y2 + 64;
view = [[UIView alloc] initWithFrame:CGRectMake(y, 0, y2, 50)];
imageName = [[NSString alloc] initWithFormat:@"c%i", (i + 1)]; // my image name is c1, c2, c3, c4 so i use this
UIImage *image1 = [UIImage imageNamed:imageName];
imageView = [[UIImageView alloc] initWithImage:image1];
[view addSubview:imageView];
[scrollview addSubview:view];
[view release];
[imageView release];
[imageName release];
}
scrollview.contentSize = CGSizeMake(viewcount*64,0); //viewcount*64 because i use image width 64..and second parameter is 0..because i not wants vertical scrolling...
[myView addSubview:scrollview];
[scrollview release];
}
I hope this helps.
我希望这有帮助。
回答by Sharme
Here I am posting my answer. Try it.
我在这里发布我的答案。尝试一下。
You can load all the images to an array. And you can design a view having one image view and try the below code:
您可以将所有图像加载到一个数组中。您可以设计一个具有一个图像视图的视图并尝试以下代码:
array name: examplearray and view name :exampleview
数组名称:examplearray 和视图名称:exampleview
-(void)updateImagesInScrollView
{
int cnt = [examplearray count];
for(int j=0; j< cnt; ++j)
{
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"exampleview"
owner:self
options:nil];
UIView *myView = [nibContents objectAtIndex:0];
exampleview * rview= (exampleview *)myView;
/* you can get your image from the array and set the image*/
rview.imageview.image = yourimage;
/*adding the view to your scrollview*/
[self.ScrollView addSubview:rview];
}
/* you need to set the content size of the scroll view */
self.ScrollView.contentSize = CGSizeMake(X, self.mHorizontalScrollView.contentSize.height);
}