1

I am adding a series of UIImageViews to a UIScrollView and I am failing as stuff is not getting centered vertically or horizonatally.

I have tried the following approach and It does not seem to be working:

In my UiTableViewController viewdidLoad:

UIImage *waitingImage = [UIImage imageWithContentsOfFile:pathOfNoImageFile];
                     UIImageView *imageview = [[UIImageView alloc] init];

              [imageview  setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:waitingImage];

                     [imageview setContentMode:UIViewContentModeScaleAspectFit];
                     self.scrollview.imageview = imageview;
                     [self.scrollview addSubview:imageview];

I am using a custom UIScrollview so that it can do the centering:

header

#import <UIKit/UIKit.h>
@interface ListingDetailScrollViewController : UIScrollView
@property (nonatomic, retain) UIImageView *imageview;
@end

implementation

#import "ListingDetailScrollViewController.h"

@implementation ListingDetailScrollViewController
@synthesize imageview=_imageview;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (void)layoutSubviews {
    [super layoutSubviews];

    // center the image as it becomes smaller than the size of the screen
    CGSize boundsSize = self.bounds.size;
    CGRect frameToCenter = self.imageview.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;

    self.imageview.frame = frameToCenter;
}

@end

Any tips or thoughts?

Strong Like Bull
  • 11,155
  • 36
  • 98
  • 169
  • You may want to take a look at the "Panning & Zooming with UIScrollView" tutorial on my site http://iosdeveloperzone.com/2012/07/07/tutorial-all-about-images-part-2-panning-zooming-with-uiscrollview/. In it I explain how to overcome some of UIScrollView's weirdness to do with centering. – idz Sep 18 '12 at 20:32
  • CGRect frameToCenter = self.imageview.frame; shouldn't it be CGRect frameToCenter = self.imageview.bounds; – tiguero Sep 18 '12 at 20:33
  • I got the tip from http://stackoverflow.com/questions/638299/uiscrollview-with-centered-uiimageview-like-photos-app which seems to be a popular answer – Strong Like Bull Sep 18 '12 at 20:46
  • @tiguero no, since he wants to position the iamgeView, hee neds to modify it's frame, which is position and size in the superviews (scrollviews) coordinate system, while bounds is the same in the receivers coordinate system. – Ahti Sep 18 '12 at 21:30

0 Answers0