英文:
iOS / ObjectiveC: I try to open a viewcontroller with an imageview containing a local image, but it displays a white page
问题
当我使用调试器时,我可以在图像视图中看到图像。为什么它显示为白色页面?(我可以看到返回按钮)
- (void) openIMAGE:(NSIndexPath *) indexPath
{
NSString *filePath = [menuPath stringByAppendingPathComponent:contentsList[indexPath.row]];
UIViewController* imageViewController = [UIViewController new];
imageViewController.view.backgroundColor = [UIColor whiteColor];
//0,21,73,30
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
;
;
forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 21.0, 73.0, 30.0);
UIImageView *imageView = [UIImageView new];
imageView.image = [UIImage imageWithContentsOfFile: filePath];
[imageViewController.view addSubview:imageView];
[imageViewController.view addSubview:button];
[self.navigationController pushViewController:imageViewController animated:YES];
}
英文:
I precise that when I use the debugger, I can see the image in the image view.
Why it displays a white page ? (I can see the back button)
- (void) openIMAGE:(NSIndexPath *) indexPath
{
NSString *filePath = [menuPath stringByAppendingPathComponent:contentsList[indexPath.row]];
UIViewController* imageViewController = [UIViewController new];
imageViewController.view.backgroundColor = [UIColor whiteColor];
//0,21,73,30
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
;
;
forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 21.0, 73.0, 30.0);
UIImageView *imageView= [UIImageView new];
imageView.image = [UIImage imageWithContentsOfFile: filePath];
// UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 48.0, self.view.frame.size.width, self.view.frame.size.height - 48.0)];
[imageViewController.view addSubview:imageView];
[imageViewController.view addSubview:button];
[self.navigationController pushViewController:imageViewController animated:YES];
}
答案1
得分: 1
UIImageView *imageView = [UIImageView new];
imageView.image = [UIImage imageWithContentsOfFile: filePath];
[imageViewController.view addSubview: imageView];
与您的按钮不同,您的imageView
没有框架和大小。因此,它是左上角的一个无穷小点,您无法看到它。
除此之外,您的代码很糟糕。永远不要从另一个视图控制器中填充视图控制器的视图。创建一个图像视图控制器类,并让它自己填充自己。
英文:
UIImageView *imageView= [UIImageView new];
imageView.image = [UIImage imageWithContentsOfFile: filePath];
[imageViewController.view addSubview:imageView];
Unlike your button, your imageView
has no frame and no size. Thus it is an infinitesimal point at the upper left corner and you can't see it.
Quite apart from that, your code is atrocious. Never populate a view controller's view from another view controller. Make an image view controller class and have it populate itself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论