RWTCenteredScrollView.h:
#import <UIKit/UIKit.h>
@interface RWTCenteredScrollView : UIScrollView
@end
RWTCenteredScrollView.m:
#import "RWTCenteredScrollView.h"
@implementation RWTCenteredScrollView
- (void)layoutSubviews {
[super layoutSubviews];
[self centerContent];
}
- (void)centerContent {
if (self.delegate && [self.delegate respondsToSelector:@selector(viewForZoomingInScrollView:)]) {
UIView *viewToCenter = [self.delegate viewForZoomingInScrollView:self];
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = viewToCenter.frame;
if (frameToCenter.size.width < boundsSize.width) {
frameToCenter.origin.x =(boundsSize.width - frameToCenter.size.width) / 2;
} else {
frameToCenter.origin.x = 0;
}
if (frameToCenter.size.height < boundsSize.height) {
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
} else {
frameToCenter.origin.y = 0;
}
viewToCenter.frame = frameToCenter;
}
}
@end