1
votes

As a newbie to iOS programming I was a bit confused about UIImageView and UIScrollView when working inside UIPopoverController. Here they are...

A UIViewController whose view point to an UIScrollView whose subview is an UIImageView. The UIImageView's image property is set by another class during UIViewController initialization.

-(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;
}

Then I put the UIViewController as contentViewController of a UIPopoverController which is then pop up in response block.

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];
            }
        };

When the block runs a Popup window shows up but no image shows, which I am sure image is properly set and contentSize of UIScrollView is also set up as image size.

When I change UIViewController's view to UIImageView directly, the image shows. I'm not sure what happens and why image in scrollview is not visible.

2
Plus, I can see scrollview is set up with horizontal and vertical scroll bar show during slide inside pop-up window.Alan Hoo

2 Answers

0
votes

Probably the difference is that the controller resizes its view to fit its container bounds, whilst you never set the frame of the image view here. Here's an example that shows how to do that. Changing to

-(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;
}

should set things up correctly.

0
votes

I've fixed it though still some confused.

Image doesn't show because I didn't set frame to UIImageView resides in UIScrollView. When I was using initWithFrame by providing CGRect data, image was showed up in designated dimension.

The thing is that if I add UIImageView as subview to an UIScrollView, then put the UIScrollView as root view of the UIWindow, in another trial project, both views are created using 'init' only without specified frame set, Image can be seen...

When should I set frame for UIView and when it is not necessary....