I am going through a book on iOS development, and i'm building a test application for the iPad. I am trying to create a popover when run on the iPad that displays a UIScrollView showing a UIImage that the user can scroll around.
The problem I am hitting is that when the popover has loaded the image has been stretched to fit the size of the popover exactly (making scrolling both useless and not working).
I cannot figure out the behavior of my application. I am creating a new UIPopoverController when the user clicks on an UIButton inside a cell on a UITableView.
ItemsViewController.m
CGRect rect = [[self view] convertRect:[sender bounds] fromView:sender];
ImageViewController *ivc = [[ImageViewController alloc] init];
[ivc setImage:img]
// Present a popover from the rect
imagePopover = [[UIPopoverController alloc] initWithContentViewController:ivc];
[imagePopover setDelegate:self];
[imagePopover setPopoverContentSize:CGSizeMake(600.0, 600.0)];
[imagePopover presentPopoverFromRect: rect inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
I am creating the UIScrollView and the UIImageView programmatically as follows inside my ImageViewController.
ImageViewController.m
- (id)init {
if (self = [super init]) {
scrollView = [[UIScrollView alloc] init];
imageView = [[UIScrollView alloc] init];
}
return self;
}
To try and figure out why the scollView is resetting its size by the time to the popover has shown on the screen, I created a small method to print out the size of the frame of the scrollView, the size of the frame of the imageView, and the content size of the scrollView.
Inside viewDidLoad I set the imageView image (and adjust the size). I also add the imageView to the scrollView.
ImageViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[imageView setImage:[self image]];
[imageView sizeToFit];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
[scrollView setContentSize:[self image].size];
[self logSizesFromMethod:_cmd];
}
I log the sizes from within viewWillAppear and viewDidAppear as well.
ImageViewController.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self logSizesFromMethod:_cmd];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self logSizesFromMethod:_cmd];
}
In the end, I get the following output from the console.
Homepwner[12188:11303] viewDidLoad - imageView.frame - Width:1400.000000, Height: 1060.000000
Homepwner[12188:11303] viewDidLoad - scrollView.frame - Width:320.000000, Height: 548.000000
Homepwner[12188:11303] viewDidLoad - scrollView.contentSize - Width:1400.000000, Height: 1060.000000
Homepwner[12188:11303] viewWillAppear: - imageView.frame - Width:1400.000000, Height: 1060.000000
Homepwner[12188:11303] viewWillAppear: - scrollView.frame - Width:320.000000, Height: 548.000000
Homepwner[12188:11303] viewWillAppear: - scrollView.contentSize - Width:1400.000000, Height: 1060.000000
Homepwner[12188:11303] viewDidAppear: - imageView.frame - Width:600.000000, Height: 600.000000
Homepwner[12188:11303] viewDidAppear: - scrollView.frame - Width:600.000000, Height: 600.000000
Homepwner[12188:11303] viewDidAppear: - scrollView.contentSize - Width:600.000000, Height: 600.000000
I can understand why the scrollView frame size is set to some default as viewDidLoad is called is before the popover knows what size it is to present itself as. That is, viewDidLoad is called before setPopoverContentSize is called inside ItemsViewController.
If I set the scrollView frame size, the imageView frame size and the scrollView contentSize manually in the viewDidAppear method, the popover appears exactly how I want until I try to scroll with the mouse, then everything immediately snaps back to 600x600.
Why is the scrollView frame size, the imageView frame size and the scrollView contentSize all reset to 600x600 after viewWillAppear but before viewDidAppear?
I am assuming I am not understanding some fundamental here, what do I need to change for the UIScrollView inside my UIPopoverController to behave the way I want?