Skip to content

Instantly share code, notes, and snippets.

@McZonk
Created February 20, 2013 17:01
Show Gist options
  • Save McZonk/4997099 to your computer and use it in GitHub Desktop.
Save McZonk/4997099 to your computer and use it in GitHub Desktop.
//
// UIPCenteredScrollView.m
// Plus
//
// Created by Maximilian Christ on 5/30/12.
// Copyright (c) 2012 mczonk.de. All rights reserved.
//
#import "UIPCenteredScrollView.h"
#import "NSArray+NSPFirstObject.h"
@implementation UIPCenteredScrollView
- (void)layoutSubviews {
[super layoutSubviews];
CGSize boundsSize = self.bounds.size;
UIView* centerView = self.subviews.firstObject;
CGRect frameToCenter = centerView.frame;
if(frameToCenter.size.width < boundsSize.width) {
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) * 0.5f;
} else {
frameToCenter.origin.x = 0;
}
if(frameToCenter.size.height < boundsSize.height) {
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) * 0.5f;
} else {
frameToCenter.origin.y = 0;
}
centerView.frame = frameToCenter;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment