RainBow.m
#import "RainBow.h"
@implementation RainBow
{
NSArray * _colorArray;
}
-(instancetype)initWithFrame:(CGRect)frame withNum:(NSInteger)
num{
self=[superinitWithFrame:frame];
if(self){
//1>
加载数据
[self_loadData];
num=num<=_colorArray.count?num:_colorArray.count;
self.backgroundColor=[UIColorwhiteColor];
//2>
加载子视图
for(int i=0;i<num;i++){
UIView * view=[[UIViewalloc]
initWithFrame:CGRectMake(0,i*10,self.frame.size.width,10)];
view.backgroundColor=_colorArray[i];
[selfaddSubview:view];
}
//3>
重新自定义当前父视图的高度
frame.size.height=10*num;
self.frame=frame;
}
returnself;
}
AppDelegate.m
#import "AppDelegate.h"
int count =0;
@interface
AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
UIWindow * window = [[UIWindow
alloc]initWithFrame:[UIScreen
mainScreen].bounds];
window.backgroundColor = [UIColor
whiteColor];
self.window = window;
[self.window
makeKeyAndVisible];
JRRainBow * view=[[JRRainBowalloc]
initWithFrame:CGRectMake(375/2.0-170/2.0,110,
170, 100)
withNum:8];
view.tag=8;
[self.windowaddSubview:view];
}