iOS / ObjectiveC: I try to open a viewcontroller with an imageview containing a local image, but it displays a white page

huangapple go评论72阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月8日 18:56:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定