Last active
August 29, 2015 13:57
-
-
Save J-ogawa/9918694 to your computer and use it in GitHub Desktop.
add content to UIScrollView, auto calc scrollView's content size.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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