5

I'm trying to add scrollbars to an IKImageView. Basically at the moment, I need some examples of a program that loads an image into a view, and if the window is too small, sets up scrollbars that do the right things...

Why can I not find these examples on the apple dev site?

Added info:

After looking at ImagekitDemo I see that evidently I DO need to embed the IKIMageView in a ScrollView. (and somehow that makes the has___Scroller properties of the IKImageView YES...)

However, now (And this is true in ImageKitDemo as well) The scroll bars are fine as long as only one (or neither) is needed. However, as soon as both are needed, and either dimension of the window is smaller than the image, BOTH scrollbars disappear.

Mouse scrolling still works.

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
Brian Postow
  • 11,709
  • 17
  • 81
  • 125

2 Answers2

0

The best place to start is the Scroll View Programming Guide. Basically, you need to put the IKImageView inside a NSScrollView. If the IKImageView size exceeds the visible rectangle of the NSScrollView, then scrollbars will appears.

The following sample uses IKImageView to perform various zoom and resizing operations.

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
  • even though the IKImageView claims that has scrollbars itself? Also, when I do that, the Image gets centered in the huge imageview and so only part of it is visible... and when the imageview is smaller than the scrollview, you end up with white backgrounds. I suppose I CAN do a normal thing with making the imageview the size of the image and have another grey view behind it... but that sort of defeats some of the purpose of the ikimageview... – Brian Postow Jan 15 '10 at 14:50
  • My apologies, I read the documentation too quickly. The "zoomImageTo..." are made to change the image rectangle, and thus the visible portion in the IKImageView. There are some properties that controls whether scrollbars are visible or not. Moreover, you can take a look at the following sample code: http://developer.apple.com/mac/library/samplecode/IKImageViewDemo/index.html – Laurent Etiemble Jan 15 '10 at 15:58
  • yeah, I've tried altering the properties,but [_imageView setHasHorizontalScroller: YES] has no effect ( [_imageView hasHorizontalScroller] returns 0 directly afterwards) And that example is what I've been working off of. It seems to show how to do everything BUT Scrollbars B-) – Brian Postow Jan 15 '10 at 16:35
0

ZoomViewController.h

@interface ZoomViewController : UIViewController <UIScrollViewDelegate>
{
    ForecastImage* detailImage; // wrapper class around UIImage (optional - could just be UIImage)

    IBOutlet UIImageView* imageView;
    IBOutlet DoubleTapScrollView* zoomableScrollView;
}

@property (readwrite, nonatomic, retain) ForecastImage* detailImage;

- (IBAction) dismissZoomViewController;

- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView;

- (void) scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale;

@end

ZoomViewController.m

@implementation ZoomViewController

@synthesize detailImage;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [imageView setImage:[self.detailImage renderedImage]];

    [zoomableScrollView setContentSize:[[imageView image] size]];
    [zoomableScrollView setMaximumZoomScale:5.0];
    [zoomableScrollView setMinimumZoomScale:0.25];
}

- (void) viewDidAppear:(BOOL)animated
{
    self.navigationItem.title = [SearchService getDisplayName:[self.detailImage forecastArea]];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc
{
    imageView.image = nil;
    self.detailImage = nil;

    [super dealloc];
}

- (IBAction) dismissZoomViewController
{
    [self dismissModalViewControllerAnimated:YES];
}

#pragma mark Pinch-n-Zoom

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imageView;
}

- (void) scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    CGSize newSize = CGSizeMake(imageView.image.size.width * scale, imageView.image.size.height * scale);
    [scrollView setContentSize:newSize];
}

@end
slf
  • 22,595
  • 11
  • 77
  • 101
  • Yeah, I've gotten scrollbars to work with an NSView, but that doesn't do zooming and rotation and croppingfor me... I'd like to get IKImageView to work... – Brian Postow Jan 21 '10 at 14:35