IOS应用开发中UITableView的应用十分广泛,但是IOS7神一样的把UITableView拍扁了,这样一来IOS6的UITableView不干了,就吵着也要被拍扁,那好吧我今天就成全了你。。。
继上回书说道初步实现了一个QQ音乐的框架,但这远远不够,我是一个追求细节的人(就像锤子科技的老罗一样),怎么可能就这就结束了呢,下一步实现以下登陆的ModalView,比较简单没啥可说的直接上代码:
1UIColor *normalColor = [UIColor colorWithRed:28/255.0green:191/255.0blue:97/255.0alpha:1];//按钮默认状态的绿色2UIColor *selectedColor = [UIColor colorWithRed:185/255.0green:227/255.0blue:214/255.0alpha:1];//按钮点击状态的淡绿色3CGFloat navigationY =0;4if(IOS_7) {5navigationY =20;6}789UINavigationBar *loginNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, navigationY,320,44)];//UINavigationBar的位置10UIButton *cancelButton =[UIButton buttonWithType:UIButtonTypeSystem];11[cancelButton setFrame:CGRectMake(0,0,40,40)];12[cancelButton setTitle:@"取消"forState:UIControlStateNormal];13[cancelButton setTitleColor:normalColor forState:UIControlStateNormal];14[[cancelButton titleLabel] setFont:[UIFont systemFontOfSize:18]];15[cancelButton setTitleColor:selectedColor forState:UIControlStateHighlighted];16[cancelButton addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];17UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];//初始化取消按钮181920UIButton *loginButton =[UIButton buttonWithType:UIButtonTypeSystem];21[loginButton setFrame:CGRectMake(0,0,40,40)];22[loginButton setTitle:@"登陆"forState:UIControlStateNormal];23[loginButton setTitleColor:normalColor forState:UIControlStateNormal];24[[loginButton titleLabel] setFont:[UIFont systemFontOfSize:18]];25[loginButton setTitleColor:selectedColor forState:UIControlStateHighlighted];26[loginButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];27UIBarButtonItem *loginItem = [[UIBarButtonItem alloc] initWithCustomView:loginButton];//初始化登陆按钮2829UIImage *image = [UIImage imageNamed:@"input_login_line"];3031UIImageView *userLineView =[[UIImageView alloc] initWithImage:image];32UIImageView *passWordLineView = [[UIImageView alloc] initWithImage:image];//输入框下面的绿线3334UITextField *userTextField = [[UITextField alloc] initWithFrame:CGRectMake(30,100+navigationY,260,30)];35[userTextField setPlaceholder:@"QQ号/手机/邮箱"];36[userTextField setClearButtonMode:UITextFieldViewModeWhileEditing];37[userTextField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];38[userTextField setReturnKeyType:UIReturnKeyNext];39[userLineView setFrame:CGRectMake(30,131+navigationY,260,1)];4041UITextField *passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(30,140+navigationY,260,30)];42[passwordTextField setPlaceholder:@"密码"];43[passwordTextField setClearButtonMode:UITextFieldViewModeWhileEditing];44[passwordTextField setKeyboardType:UIKeyboardTypeASCIICapable];45[passwordTextField setReturnKeyType:UIReturnKeyDone];46[passwordLineView setFrame:CGRectMake(30,171+navigationY,260,1)];4748UILabel *regStr = [[UILabel alloc] initWithFrame:CGRectMake(75,180+navigationY,110,30)];49[regStr setText:@"没有账号?点击这里"];50[regStr setFont:[UIFont systemFontOfSize:12]];51[regStr setTextColor:[UIColor darkGrayColor]];52UILabel *greenStr = [[UILabel alloc] initWithFrame:CGRectMake(184,180+navigationY,60,30)];53[greenStr setText:@"快速注册"];54[greenStr setFont:[UIFont systemFontOfSize:12]];55[greenStr setTextColor:normalColor];5657UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"登陆"];58[navigationItem setLeftBarButtonItem:cancelItem];59[navigationItem setRightBarButtonItem:loginItem];60[loginNavigationBar pushNavigationItem:navigationItem animated:YES];61626364[self.view addSubview:loginNavigationBar];65[self.view addSubview:userTextField];66[self.view addSubview:userLineView];67[self.view addSubview:passwordTextField];68[self.view addSubview:passwordLineView];69[self.view addSubview:regStr];70[self.view addSubview:greenStr];
总结起来就是一个UINavigationBar,UINavigationBar上面有两个按钮,两个UITextField。只得说的事这个按钮,IOS7中把按钮的衣服扒掉了,这就直接导致我也要让IOS6的按钮也裸奔:
1[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(1,44)] forBarMetrics:UIBarMetricsDefault];
好了然后就没什么特别的了改改UITextField关联的键盘字体大小颜色什么的,效果图如下:
下面就是硬菜了,QQ音乐的界面UITableView采用的是UITableViewStyleGrouped类型的,但是在IOS6下UITableView有一个讨厌的灰不拉几的背景,我要把背景变为白色,使用如下代码:
1[tableView setBackgroundColor:[UIColor whiteColor]];2[tableView setBackgroundView:nil];
这样UITableView在IOS6中那讨厌的背景就没有了,但是UITableViewCell默认是圆角的很恶心,干掉:
1UIView *tempView =[[UIView alloc] init];2[cell setBackgroundView:tempView];
还要去掉cell点击时的圆角:
1cell.selectedBackgroundView =[[UIView alloc] initWithFrame:cell.frame];2cell.selectedBackgroundView.backgroundColor = [UIColor lightGrayColor];
这里可以不用设置颜色的,而且现在这个颜色挺难看的,我还要继续修改
还有个问题IOS6的UITableViewCell默认不是与屏幕同宽的,需要自定义一个UItableViewCell复写 layoutSubviews 方法,代码如下:
1- (void) layoutSubviews {2[super layoutSubviews];3self.backgroundView.frame = CGRectMake(0,0,320,44);4self.selectedBackgroundView.frame = CGRectMake(0,0,320,44);5}
好了这样初步的适配完了,当然还有细节没有做到,继续学习吧,下面是我的音乐视图控制器的完整代码:
1//2//MyMusicViewController.m3//QQMusic4//5//Created by 赵 福成 on 14-5-21.6//Copyright (c) 2014年 ZhaoFucheng. All rights reserved.7//89#import"MyMusicViewController.h"10#import"UIImage+CustomPureColor.h"11#import"UIButton+CustomButton.h"12#import"LoginViewController.h"13#import"CustomTableViewCell.h"14#defineIOS_7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)15@interface MyMusicViewController ()1617@end1819@implementation MyMusicViewController2021NSArray *mainScreenCells;22NSArray *details;23NSArray *icon;24NSArray *iconSelected;2526- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil27{28self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];29if(self) {30//Custom initialization3132self.title =@"我的音乐";33}34returnself;35}3637- (void)viewDidLoad38{39[super viewDidLoad];40//Do any additional setup after loading the view.414243//我的音乐按钮44UIButton *myMusicButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,35,35)];45[myMusicButton setImage:[UIImage imageNamed:@"defaultSinger"] forState:UIControlStateNormal];46[myMusicButton setImage:[UIImage imageNamed:@"defaultSinger"] forState:UIControlStateHighlighted];47//myMusicButton.userInteractionEnabled = NO;48myMusicButton.titleLabel.textAlignment =NSTextAlignmentLeft;49[myMusicButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];50UIBarButtonItem *myMusicButtonItem =[[UIBarButtonItem alloc] initWithCustomView:myMusicButton];5152//播放界面按钮53UIBarButtonItem *nowPlayingButtonItem =[[UIBarButtonItem alloc] initWithCustomView:[UIButton initNowPlayingButton]];54[self.navigationItem setLeftBarButtonItem:myMusicButtonItem];55[self.navigationItem setRightBarButtonItem:nowPlayingButtonItem];5657mainScreenCells = [NSArray arrayWithObjects:[NSArray arrayWithObjects:@"全部歌曲", nil],[NSArray arrayWithObjects:@"我喜欢",@"全部歌单", nil],[NSArray arrayWithObjects:@"下载歌曲",@"最近播放",@"ipod歌曲", nil], nil];5859details = [NSArray arrayWithObjects:[NSArray arrayWithObjects:@"0首在本地", nil], [NSArray arrayWithObjects:@"",@"", nil],[NSArray arrayWithObjects:@"0首",@"18首",@"0首", nil], nil];6061icon = [NSArray arrayWithObjects:[NSArray arrayWithObjects:@"allsongs", nil], [NSArray arrayWithObjects:@"cell_like_in_my_music",@"", nil],[NSArray arrayWithObjects:@"down",@"recent_listen_icon",@"ipod", nil], nil];6263iconSelected = [NSArray arrayWithObjects:[NSArray arrayWithObjects:@"allsongsSelected", nil], [NSArray arrayWithObjects:@"cell_like_in_my_music_PRessed",@"", nil],[NSArray arrayWithObjects:@"downSelected",@"recent_listen_icon_h",@"ipodSelected", nil], nil];6465UITableView *tableView =[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];66[tableView setDataSource:self];67if(!IOS_7) {68[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];69}70[tableView setBackgroundColor:[UIColor whiteColor]];71[tableView setBackgroundView:nil];72[tableView setDelegate:self];73[self.view addSubview:tableView];7475}7677- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView78{79returnmainScreenCells.count;80}8182- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section83{84return[[mainScreenCells objectAtIndex:section] count];85}8687- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath88{89staticNSString *cellId =@"cellId";90UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];91if(cell ==nil) {92cell =[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];93}94NSUInteger rowNo =indexPath.row;95NSUInteger colNo =indexPath.section;96cell.textLabel.text =[[mainScreenCells objectAtIndex:colNo] objectAtIndex:rowNo];97if(!IOS_7) {98UIView *line = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.origin.x +50, cell.frame.size.height, cell.frame.size.width -20,0.5)];99line.backgroundColor =[UIColor lightGrayColor];100[cell.contentView addSubview:line];101}102NSString *iconStr = (NSString *)[[icon objectAtIndex:colNo] objectAtIndex:rowNo];103NSString *iconSelectStr = (NSString *)[[iconSelected objectAtIndex:colNo] objectAtIndex:rowNo];104if(iconStr.length ==0&& iconSelectStr.length ==0) {105cell.imageView.image = [UIImage imageWithColor:[UIColor clearColor] size:CGSizeMake(50,56)];106cell.imageView.highlightedImage = [UIImage imageWithColor:[UIColor clearColor] size:CGSizeMake(50,56)];107}108else109{110cell.imageView.image =[UIImage imageNamed:iconStr];111cell.imageView.highlightedImage =[UIImage imageNamed:iconSelectStr];112}113114cell.detailTextLabel.text =[[details objectAtIndex:colNo] objectAtIndex:rowNo];115//[cell setaccessoryType:UITableViewCellAccessoryDisclosureIndicator];116[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow"]]];117118//设置标题和描述背景透明119cell.textLabel.backgroundColor =[UIColor clearColor];120cell.detailTextLabel.backgroundColor =[UIColor clearColor];121122//去掉cell的圆角123UIView *tempView =[[UIView alloc] init];124[cell setBackgroundView:tempView];125126//cell点击时的颜色127cell.selectedBackgroundView =[[UIView alloc] initWithFrame:cell.frame];128cell.selectedBackgroundView.backgroundColor =[UIColor lightGrayColor];129130returncell;131}132133134- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath135{136UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];137[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_gedan"]]];138}139140- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath141{142UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];143[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow"]]];144145}146147- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section148{149return0.1;150}151152- (void)login:(UIButton *)sender153{154LoginViewController *loginView =[[LoginViewController alloc] init];155[self presentViewController:loginView animated:YES completion:^{156157}];158}159160- (void)didReceiveMemoryWarning161{162[super didReceiveMemoryWarning];163//Dispose of any resources that can be recreated.164}165166167168/*169#pragma mark - Navigation170171// In a storyboard-basedapplication, you will often want to do a little preparation before navigation172- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender173{174// Get the new view controller using [segue destinationViewController].175// Pass the selected object to the new view controller.176}177*/178179@end
再来张效果图:
睡觉去了。。。。。。