【问题标题】:An image embedded in UIScrollView doesn't show in UIPopoverControllerUIScrollView 中嵌入的图像未显示在 UIPopoverController 中
【发布时间】:2016-05-30 23:08:03
【问题描述】:

作为 iOS 编程的新手,在 UIPopoverController 中工作时,我对 UIImageView 和 UIScrollView 有点困惑。他们来了……

一个 UIViewController,它的视图指向一个 UIScrollView,它的子视图是一个 UIImageView。 UIImageView 的 image 属性是在 UIViewController 初始化期间由另一个类设置的。

-(void)loadView{
    self.scrollView=[[UIScrollView alloc]init];
    self.imageView=[[UIImageView alloc]init];
    self.imageView.contentMode=UIViewContentModeScaleAspectFit;
    [self.scrollView addSubview:self.imageView];
    self.view=self.scrollView;
}

-(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];
    self.imageView.image=self.image;
    self.scrollView.contentSize=self.imageView.image.size;
}

然后我将 UIViewController 作为 UIPopoverController 的 contentViewController,然后在响应块中弹出。

cell.actionBlock=^{
            NSLog(@"Going to show image for %@", item);
            BNRItemCell* strongCell=weakCell;
            if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad){
                NSString* itemKey=item.itemKey;
                UIImage* img=[[BNRImageStore sharedStore]imageForKey:itemKey];
                if(!img){
                    return;
                }
                CGRect rect=[self.view convertRect:strongCell.thumbnailView.bounds fromView:strongCell.thumbnailView];

                BNRImageViewController* ivc=[[BNRImageViewController alloc]init];
                ivc.image=img;
                self.imagePopover=[[UIPopoverController alloc]initWithContentViewController:ivc];
                self.imagePopover.delegate=self;
                self.imagePopover.popoverContentSize=CGSizeMake(600,600);
                [self.imagePopover presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
            }
        };

当块运行时会显示一个弹出窗口但没有显示图像,我确信图像已正确设置并且 UIScrollView 的 contentSize 也设置为图像大小。

当我将 UIViewController 的视图直接更改为 UIImageView 时,图像会显示。我不确定会发生什么以及为什么滚动视图中的图像不可见。

【问题讨论】:

  • 另外,我可以看到滚动视图设置为在弹出窗口内滑动期间显示水平和垂直滚动条。

标签: ios objective-c uipopovercontroller


【解决方案1】:

可能不同之处在于控制器调整其view 的大小以适应其容器边界,而您从未在此处设置图像视图的框架。 Here's an example 展示了如何做到这一点。改为

-(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];
    self.imageView.image = self.image;
    self.imageView.frame.size = self.image.size;
    self.scrollView.contentSize = self.imageView.frame.size;
}

应该正确设置。

【讨论】:

  • 试过但还是不行......self.imageView.frame.size是一个只读属性......
  • 对,在 Swift 中可以使用。在 newFrame CGRect 中设置宽度和高度,然后在 Objective-C 中重新分配。
【解决方案2】:

我已经修复了它,但仍然有些困惑。

图像不显示,因为我没有将框架设置为 UIImageView 驻留在 UIScrollView 中。当我通过提供CGRect数据使用initWithFrame时,图像显示在指定的维度。

问题是,如果我将 UIImageView 作为子视图添加到 UIScrollView,然后将 UIScrollView 作为 UIWindow 的根视图,在另一个试验项目中,两个视图都是使用“init”创建的,仅没有指定的框架集,图像可以看过...

什么时候应该为 UIView 设置框架,什么时候不需要......

【讨论】:

  • 严格来说,在现代代码中,您永远不应该为 UIView 设置框架。您应该始终创建自动布局约束并让它为您确定框架。在 UIImageView 的特定情况下,其自动布局的固有大小是您提供的图像的大小,因此布局大部分都可以正常工作。默认视图通常具有较旧的 springs 和 struts 布局行为,它们在布局时转换为自动布局约束。
猜你喜欢
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 2021-12-11
  • 2017-06-11
相关资源
最近更新 更多