标签:ios uiscrollview 自动调整
分享个能够自动适应大小的ScrollView
下面贴下适应的代码
ViewController.m
#import "ViewController.h" #import "AutoAdaptationScrollView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. AutoAdaptationScrollView *autoSizeView = [[AutoAdaptationScrollView alloc] init]; // 设置ScrollView的方向 autoSizeView.directionType = AutoAdaptationScrollViewPortrait; autoSizeView.frame = CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50); autoSizeView.backgroundColor = [UIColor grayColor]; [self.view addSubview:autoSizeView]; // 内容的View UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(autoSizeView.frame), 900)]; [autoSizeView addSubview:contentView]; //子视图1 UIView *AView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(autoSizeView.frame), 370)]; AView.backgroundColor = [UIColor blueColor]; [contentView addSubview:AView]; //子视图1 UIView *BView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(AView.frame), CGRectGetWidth(autoSizeView.frame), 370)]; BView.backgroundColor = [UIColor greenColor]; [contentView addSubview:BView]; //让ScrollView的大小适应内容视图 [autoSizeView autoAdaptationWithView:contentView]; }
#import <UIKit/UIKit.h> typedef enum { AutoAdaptationScrollViewPortrait, AutoAdaptationScrollViewLandscape }AutoAdaptationScrollViewDirection; @interface AutoAdaptationScrollView : UIScrollView @property (nonatomic , assign) AutoAdaptationScrollViewDirection directionType; - (void)autoAdaptationWithView:(UIView *)view; @endAutoAdaptationScrollView.m
#import "AutoAdaptationScrollView.h" @implementation AutoAdaptationScrollView - (id) init { if (self = [super init]) { _directionType = AutoAdaptationScrollViewPortrait; } return self; } - (id) initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _directionType = AutoAdaptationScrollViewPortrait; } return self; } - (void)autoAdaptationWithView:(UIView *)view { if (_directionType == AutoAdaptationScrollViewPortrait) { NSArray *subviews = view.subviews; __block UIView *lastView; [UIView animateWithDuration:0.5f animations:^{ for (UIView *view in subviews) { [self setViewY:CGRectGetMaxY(lastView.frame) view:view]; lastView = view; } [self setContentSize:CGSizeMake(0.0f, CGRectGetMaxY(lastView.frame))]; }]; }else if (_directionType == AutoAdaptationScrollViewLandscape) { NSArray *subviews = view.subviews; __block UIView *lastView; [UIView animateWithDuration:0.5f animations:^{ for (UIView *view in subviews) { [self setViewX:CGRectGetMaxX(lastView.frame) view:view]; lastView = view; } [self setContentSize:CGSizeMake(CGRectGetMaxX(lastView.frame), 0.0f)]; }]; } } // 设置UIView的Y - (void)setViewY:(CGFloat)newY view:(UIView *)mView { CGRect viewFrame = [mView frame]; viewFrame.origin.y = newY; [mView setFrame:viewFrame]; } // 设置UIView的X - (void)setViewX:(CGFloat)newX view:(UIView *)mView { CGRect viewFrame = [mView frame]; viewFrame.origin.x = newX; [mView setFrame:viewFrame]; } @end
代码下载 http://download.csdn.net/detail/qqmcy/8460365
AutoAdaptationScrollView能够适应自动内容大小的ScrollView
标签:ios uiscrollview 自动调整
原文地址:http://blog.csdn.net/qqmcy/article/details/43966005