思路按照起始点位置和宽高比例进行button 的添加 1 // 2 // TableController.m 3 // hedaAssistant 4 // 5 // Created by bear on 15/11/28. 6 // Copyright © 2015年 bear. All rights reserved. 7 // 8 9 #import "TableController.h" 10 #import "TableCellController.h" 11 #import "UINavigationItem+custom.h" 12 13 14 //获取屏幕 宽度、高度 15 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) 16 #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) 17 18 19 //NavBar高度 20 #define NavigationBar_HEIGHT 44 21 #define StatusBar_HEIGHT 20 22 #define TabBar_HEIGHT 49 23 24 @interface TableController (){ 25 UIScrollView *tableScrowView; 26 CGFloat dayWidth; 27 CGFloat sectionHeight; 28 29 }@end 30 31 @implementation TableController 32 -(void)viewDidLoad{ 33 34 35 [self setUI]; 36 37 } 38 39 40 41 -(void)setUI{ 42 43 44 //设置 导航栏的背景 45 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tabbar_background.png"] forBarMetrics:UIBarMetricsDefault]; 46 47 //设置自己的标题x 48 [self.navigationItem setMyTitle:@"考试安排表"]; 49 50 51 52 53 54 55 //添加整体背景 56 57 UIImage *bgImg=[UIImage imageNamed:@"main.png"]; 58 UIImageView *mainBgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-(StatusBar_HEIGHT+NavigationBar_HEIGHT+TabBar_HEIGHT))]; 59 mainBgView.image=bgImg; 60 [self.view addSubview:mainBgView]; 61 62 //添加顶部星期背景 63 UIView *headBg=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)]; 64 [headBg setAlpha:0.7f]; 65 headBg.backgroundColor=[UIColor orangeColor]; 66 [self.view addSubview:headBg]; 67 68 69 //定义基本宽高 70 dayWidth=(CGFloat) (SCREEN_WIDTH-30)/5; 71 sectionHeight=(CGFloat)(SCREEN_HEIGHT-(StatusBar_HEIGHT+NavigationBar_HEIGHT+TabBar_HEIGHT)-30)/8; 72 73 74 #pragma mark 添加ScrowView能左右滚动 75 tableScrowView=[[UIScrollView alloc]init]; 76 tableScrowView.frame=CGRectMake(0, 0, SCREEN_WIDTH,sectionHeight*8+35); 77 tableScrowView.contentSize = CGSizeMake(dayWidth*7+30, 0); 78 tableScrowView.bounces=NO; 79 [self.view addSubview:tableScrowView]; 80 81 82 #pragma mark 添加星期头部 83 84 int i; 85 NSString *weektitle[]={@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",@"星期日"}; 86 for(i =0; i<7;i++) 87 { 88 UIButton *buttonview = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 89 //为button显示赋值 90 91 [buttonview setTitle:weektitle[i] forState:UIControlStateNormal]; 92 [buttonview setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 93 //设置button的大小 94 buttonview.frame = CGRectMake(30+i*dayWidth, 0, dayWidth, 30); 95 [tableScrowView addSubview:buttonview]; 96 97 98 99 100 } 101 102 103 #pragma mark 添加左边节数 标示 104 UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 30, 30, sectionHeight*8)]; 105 [self.view addSubview:sectionView]; 106 for(i =0; i<8;i++) 107 { 108 //新建一个button对象 button还有一些别的属性比如背景色 109 NSString *title=[NSString stringWithFormat:@"%d",i+1]; 110 UIButton *buttonview = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 111 //为button显示赋值 112 buttonview.backgroundColor=[UIColor lightGrayColor]; 113 if (i%2) { 114 [buttonview setAlpha:0.5f]; 115 }else{ 116 [buttonview setAlpha:0.7f]; 117 } 118 119 [buttonview setTitle:title forState:UIControlStateNormal]; 120 [buttonview setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 121 //设置button的大小 122 buttonview.frame = CGRectMake(0, i*sectionHeight, 30, sectionHeight); 123 [sectionView addSubview:buttonview]; 124 125 126 } 127 128 129 130 #pragma mark 加号分割点添加 131 int j; 132 for (i=0; i<8; i++) { 133 for (j=0; j<7; j++) { 134 UILabel *lable=[[UILabel alloc]init]; 135 [lable setText:@"+"]; 136 [lable setTextColor:[UIColor lightGrayColor]]; 137 lable.frame=CGRectMake(30+dayWidth*i, 30+sectionHeight*j, 10, 10); 138 lable.center=CGPointMake(30+dayWidth*i, sectionHeight*(j+1.5)); 139 [tableScrowView addSubview:lable]; 140 } 141 } 142 143 144 145 146 147 148 149 150 //添加Lesson 151 [self addLesson]; 152 153 } 154 155 156 157 158 #pragma mark 画课表按钮 每节课按照数组画出来 159 -(void)addLesson{ 160 161 162 // 以下为测试数据 163 UIButton *lessonBUtton=[[UIButton alloc]initWithFrame:CGRectMake(30, 30, dayWidth, sectionHeight*2)]; 164 [lessonBUtton setTitle:@"ios程序设计" forState:UIControlStateNormal]; 165 lessonBUtton.titleLabel.numberOfLines=0; 166 [lessonBUtton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 167 [lessonBUtton setBackgroundColor:[UIColor redColor]]; 168 [lessonBUtton.layer setCornerRadius:8.0]; 169 [lessonBUtton setAlpha:0.7f]; 170 [lessonBUtton addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 171 [tableScrowView addSubview:lessonBUtton]; 172 173 174 175 UIButton *lessonBUtton1=[[UIButton alloc]initWithFrame:CGRectMake(30, 30+sectionHeight*4, dayWidth, sectionHeight*2)]; 176 [lessonBUtton1 setTitle:@"专业英语" forState:UIControlStateNormal]; 177 lessonBUtton1.titleLabel.numberOfLines=0; 178 [lessonBUtton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 179 [lessonBUtton1 setBackgroundColor:[UIColor purpleColor]]; 180 [lessonBUtton1.layer setCornerRadius:8.0]; 181 [lessonBUtton1 setAlpha:0.7f]; 182 [lessonBUtton1 addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 183 [tableScrowView addSubview:lessonBUtton1]; 184 185 186 UIButton *lessonBUtton12=[[UIButton alloc]initWithFrame:CGRectMake(30+dayWidth*2, 30+sectionHeight*3, dayWidth, sectionHeight*2)]; 187 [lessonBUtton12 setTitle:@"概率论" forState:UIControlStateNormal]; 188 lessonBUtton12.titleLabel.numberOfLines=0; 189 [lessonBUtton12 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 190 [lessonBUtton12 setBackgroundColor:[UIColor blueColor]]; 191 [lessonBUtton12.layer setCornerRadius:8.0]; 192 [lessonBUtton12 setAlpha:0.7f]; 193 [lessonBUtton12 addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 194 [tableScrowView addSubview:lessonBUtton12]; 195 196 197 UIButton *lessonBUtton135=[[UIButton alloc]initWithFrame:CGRectMake(30+dayWidth*4, 30+sectionHeight*0, dayWidth, sectionHeight*3)]; 198 [lessonBUtton135 setTitle:@"疯玩,疯睡。哈哈哈" forState:UIControlStateNormal]; 199 lessonBUtton135.titleLabel.numberOfLines=0; 200 [lessonBUtton135 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 201 [lessonBUtton135 setBackgroundColor:[UIColor brownColor]]; 202 [lessonBUtton135.layer setCornerRadius:8.0]; 203 [lessonBUtton135 setAlpha:0.7f]; 204 [lessonBUtton135 addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 205 [tableScrowView addSubview:lessonBUtton135]; 206 207 208 209 UIButton *lessonBUtton15=[[UIButton alloc]initWithFrame:CGRectMake(30+dayWidth*5, 30+sectionHeight*1, dayWidth, sectionHeight*2)]; 210 [lessonBUtton15 setTitle:@"专业英语" forState:UIControlStateNormal]; 211 lessonBUtton15.titleLabel.numberOfLines=0; 212 [lessonBUtton15 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 213 [lessonBUtton15 setBackgroundColor:[UIColor purpleColor]]; 214 [lessonBUtton15.layer setCornerRadius:8.0]; 215 [lessonBUtton15 setAlpha:0.7f]; 216 [lessonBUtton15 addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 217 [tableScrowView addSubview:lessonBUtton15]; 218 219 UIButton *lessonBUtton123=[[UIButton alloc]initWithFrame:CGRectMake(30+dayWidth*6, 30+sectionHeight*5, dayWidth, sectionHeight*2)]; 220 [lessonBUtton123 setTitle:@"睡觉,不写了" forState:UIControlStateNormal]; 221 lessonBUtton123.titleLabel.numberOfLines=0; 222 [lessonBUtton123 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 223 [lessonBUtton123 setBackgroundColor:[UIColor orangeColor]]; 224 [lessonBUtton123.layer setCornerRadius:8.0]; 225 [lessonBUtton123 setAlpha:0.7f]; 226 [lessonBUtton123 addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 227 [tableScrowView addSubview:lessonBUtton123]; 228 229 230 // 231 232 // 此处循环创建的Button 可由tag 取出 233 // for (int i=0 ; i<6; i++) { 234 // UIButton *button=[UIButton alloc]; 235 // button.tag=4; 236 // [tableScrowView addSubview:button]; 237 // 238 // 239 // } 240 // 241 242 243 } 244 245 246 247 248 -(void)cellClick:(UIButton*) _button{ 249 250 TableCellController *tableCell=[[TableCellController alloc]init]; 251 252 tableCell.button=_button; 253 UINavigationController *navTableCell=[[UINavigationController alloc]initWithRootViewController:tableCell]; 254 255 self.view.window.rootViewController = navTableCell; 256 257 258 259 } 260 261 262 // 263 //TableCellController *tableCell=[[TableCellController alloc]init]; 264 // 265 //tableCell.button=_button; 266 //UINavigationController *navTableCell=[[UINavigationController alloc]initWithRootViewController:tableCell]; 267 //[UIView transitionFromView:self.view.window.rootViewController.view 268 // toView:navTableCell.view 269 // duration:0.5 270 // options:UIViewAnimationOptionTransitionCurlUp 271 // completion:^(BOOL finished) 272 // { 273 // self.view.window.rootViewController = navTableCell; 274 // }]; 275 276 277 278 @end
源码下载地址 https://github.com/xiaocaiabc/hedaAssistant.git
时间: 2024-10-14 16:23:07