2016-05-29 70 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的特定情況下,其自動佈局的內在大小是您提供的圖像的大小,因此佈局大多隻會起作用。默認視圖通常具有較老的彈簧和支柱佈局行爲,在佈局時將其轉換爲自動佈局約束。 –