2016-05-29 31 views
1

作为iOS编程的新手,我在UIPopoverController内部工作时对UIImageView和UIScrollView有些困惑。在这里他们是...嵌入在UIScrollView中的图像不会在UIPopoverController中显示

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

-(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时,图像显示。我不确定发生了什么,为什么滚动视图中的图像不可见。

+0

另外,我可以看到滚动视图是内弹出动过程中,建立了具有水平和垂直滚动条显示窗口。 –

回答

0

可能不同之处在于控制器调整其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; 
} 

应正确设置。

+0

尝试过,但仍然不能正常工作.... self.imageView.frame.size是一个只读属性..... –

+0

对,在Swift中有效。在'newFrame' CGRect中设置宽度和高度,然后在Objective-C中分配它。 –

0

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

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

问题是,如果我将UIImageView作为子视图添加到UIScrollView,然后将UIScrollView作为UIWindow的根视图,在另一个试验项目中,两个视图都是使用'init'创建的,而没有指定帧集,Image can可以看出...

我什么时候应该设定UIView的框架,当它是没有必要的....

+0

严格地说,在现代代码中你不应该为UIView设置框架。您应该始终创建自动布局约束,并让它为您确定框架。在UIImageView的特定情况下,其自动布局的内在大小是您提供的图像的大小,因此布局大多只会起作用。默认视图通常具有较老的弹簧和支柱布局行为,在布局时将其转换为自动布局约束。 –