Iphone Addsubview不显示uiimageview

Iphone Addsubview不显示uiimageview,iphone,uiscrollview,Iphone,Uiscrollview,我试图通过AddSubView将uiimageview放在scrollview上,但没有显示任何内容。这是viewDidLoad中的代码: UIImage *buttonImage = [UIImage imageNamed:@"button"]; UIImageView *buttonImageview = [[UIImageView alloc] initWithImage:buttonImage]; buttonImageview.frame = (CGRect){.origin=CGPo

我试图通过AddSubView将uiimageview放在scrollview上,但没有显示任何内容。这是viewDidLoad中的代码:

UIImage *buttonImage = [UIImage imageNamed:@"button"];
UIImageView *buttonImageview = [[UIImageView alloc] initWithImage:buttonImage];
buttonImageview.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=buttonImage.size};
[self.scrollView addSubview:buttonImageview];
我做错了什么?

我肯定你的意思是

[UIImage imageNamed:@"button.png"]
;-)

我的想法: -确保文件确实在编译资源中。 -通过一些NSLog检查图像是否正确加载,打印图像的CGSize。 -确保正确设置机架


您确实可以调用[UIImage ImageName:@“button”],它会根据设备查找正确的视网膜图像或不正确的图像

Scrollview要求您在其上设置许多属性。特别是内容大小。您没有在原始问题中包含代码,您在哪里分配和设置代码,这很可能就是问题所在。没有看到它,我只能假设,但你将不得不做如下的事情。例如:

aScrollView.contentSize = sizeOfImage; // Have to set a size here related to your content.
// You should set these.
aScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[aScrollView addSubview:Your Image];

aScrollView.minimumZoomScale = .5;  // You will have to set some values for these
aScrollView.maximumZoomScale = 2.0;
aScrollView.zoomScale = 1;

[self.view addSubview:aScrollView];    

我发现了问题所在:(在我的.h文件中,scrollview的IBOutlet所在的行中填充了圆圈,这意味着它已经连接好了。我将光标放在它上面,以查看哪里没有显示任何内容。我再次连接它,它现在正在工作。

我没有放置扩展名,以便它选择button.png或button.pngbutton@2x.png.@您是否尝试将其添加到非scroll视图?是的,但说到阅读文档:)“在iOS 4及更高版本上,如果文件为PNG格式,则无需指定.PNG文件扩展名。在iOS 4之前,必须指定文件扩展名。”