Skip to content

Instantly share code, notes, and snippets.

@J-ogawa
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save J-ogawa/9918694 to your computer and use it in GitHub Desktop.
Save J-ogawa/9918694 to your computer and use it in GitHub Desktop.
add content to UIScrollView, auto calc scrollView's content size.
//
// UIScrollView+addView.m
//
// Created by J_ogawa on 2013/12/24.
// Copyright (c) 2013年 J_ogawa. All rights reserved.
//
#import "UIScrollView+addView.h"
@implementation UIScrollView (addView)
- (void)addView:(UIView *)view isHorizontal:(BOOL)isHorizontal{
//Shrink addView to scrollView frame
view.frame = (isHorizontal) ? CGRectMake(self.contentSize.width, 0, view.frame.size.width, view.frame.size.height)
: CGRectMake(0, self.contentSize.height, view.frame.size.width, view.frame.size.height);
//Expand contentSize
self.contentSize = (isHorizontal) ? CGSizeMake(self.contentSize.width + view.frame.size.width, self.contentSize.height)
: CGSizeMake(self.contentSize.width, self.contentSize.height + view.frame.size.height);
[self addSubview:view];
}
- (void)addView:(UIView *)view isHorizontal:(BOOL)isHorizontal padding:(CGFloat)padding
{
//Make padding space to add blank view
if (!(self.contentSize.width == 0.0 && self.contentSize.height == 0.0)) {
UIView *space = [[UIView alloc]initWithFrame:CGRectMake(0, 0, padding, padding)];
[self addView:space isHorizontal:isHorizontal];
}
[self addView:view isHorizontal:isHorizontal];
}
- (void)removeAllAddedContent
{
for (UIView *view in self.subviews) {
[view removeFromSuperview];
self.contentSize = CGSizeMake(self.contentSize.width - view.frame.size.width, self.contentSize.height);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment