Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.