////VIEWCONTROLLER.M//Cdtableviewheaderdemo////Created by Alex on 15/8/21.//Copyright (c) 2015 Alex. All rights reserved.//#defineKscreen_width [UIScreen mainscreen].bounds.size.width#definekscreen_height [UIScreen mainscreen].bounds.size.height#import "ViewController.h"ConstCGFloat backgroupheight = $;ConstCGFloat headimageheight= the;@interfaceViewcontroller () <UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>{UITableView*Demotableview; Uiimageview*IMAGEBG; UIView*Bgview; Uiimageview*Headimageview; UILabel*Namelabel; UILabel*Titlelabel; UIButton*leftbtn; UIButton*rightbtn;}@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Self.automaticallyadjustsscrollviewinsets=NO; [Self.navigationController.navigationBar Setshadowimage:[[uiimage alloc] init]]; [Self setupui];}-(void) setupui{Demotableview=[[UITableView alloc]init]; Demotableview.Delegate=Self ; Demotableview.datasource=Self ; [Demotableview Registerclass:[uitableviewcellclass] Forcellreuseidentifier:@"Cell"]; Demotableview.frame=[UIScreen mainscreen].bounds; Demotableview.contentinset=uiedgeinsetsmake (Backgroupheight,0,0,0); [Self.view Addsubview:demotableview]; //imagebg=[[Uiimageview alloc]init]; Imagebg.frame=cgrectmake (0, -backgroupheight, Kscreen_width, backgroupheight); Imagebg.image=[uiimage imagenamed:@"bgimage.jpg"]; [Demotableview ADDSUBVIEW:IMAGEBG];//Demotableview.tableheaderview = IMAGEBG; //bgview=[[UIView alloc]init]; Bgview.backgroundcolor=[Uicolor Clearcolor]; Bgview.frame=cgrectmake (0, -backgroupheight, Kscreen_width, backgroupheight); [Demotableview Addsubview:bgview]; //headimageview=[[Uiimageview alloc]init]; Headimageview.image=[uiimage imagenamed:@"myheadimage.jpg"]; Headimageview.frame=cgrectmake ((kscreen_width-headimageheight)/2, -, Headimageheight, headimageheight); [Bgview Addsubview:headimageview]; //Namelabel=[[UILabel alloc]init]; Namelabel.text=@"Alex"; Namelabel.textalignment=Nstextalignmentcenter; Namelabel.frame=cgrectmake ((kscreen_width-headimageheight)/2, Cgrectgetmaxy (Headimageview.frame), Headimageheight, -); Namelabel.backgroundcolor=[Uicolor Whitecolor]; [Bgview Addsubview:namelabel]; Titlelabel=[[uilabel Alloc]initwithframe:cgrectmake (0,0, -, -)]; Titlelabel.textcolor=[Uicolor Whitecolor]; Titlelabel.text=@"title"; Titlelabel.textalignment=Nstextalignmentcenter; Self.navigationItem.titleView=Titlelabel; Titlelabel.alpha=0; LEFTBTN=[[uibutton Alloc]initwithframe:cgrectmake (0,0, +, -)]; [Leftbtn settitle:@" Left"Forstate:uicontrolstatenormal]; [Leftbtn Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal]; [Leftbtn addtarget:self Action: @selector (Leftbtnaction) forcontrolevents:uicontroleventtouchupinside]; Uibarbuttonitem*leftitem=[[Uibarbuttonitem alloc]initwithcustomview:leftbtn]; Self.navigationItem.leftBarButtonItem=Leftitem; RIGHTBTN=[[uibutton Alloc]initwithframe:cgrectmake (0,0, +, -)]; [Rightbtn settitle:@" Right"Forstate:uicontrolstatenormal]; [Rightbtn Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal]; [Rightbtn addtarget:self Action: @selector (Rightbtnaction) forcontrolevents:uicontroleventtouchupinside]; Uibarbuttonitem*rightitem=[[Uibarbuttonitem alloc]initwithcustomview:rightbtn]; Self.navigationItem.rightBarButtonItem=Rightitem; }-(void) leftbtnaction{NSLog (@"Leftclick");}-(void) rightbtnaction{NSLog (@"RightClick");}#pragmamark-tableviewdelegate&datasource-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return -;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{UITableViewCell*cell=[tableview Dequeuereusablecellwithidentifier:@"Cell"Forindexpath:indexpath]; if(cell==Nil) {Cell=[[UITableViewCell alloc]init]; } returncell;}-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{cgfloat yoffset=Scrollview.contentoffset.y; CGFloat Xoffset= (Yoffset + backgroupheight)/2; NSLog (@"%f", Yoffset); if(Yoffset <-backgroupheight) {CGRect rect=Imagebg.frame; RECT.ORIGIN.Y=Yoffset; Rect.size.height= -Yoffset; Rect.origin.x=Xoffset; Rect.size.width= Kscreen_width + fabs (xoffset) *2; Imagebg.frame=rect; } cgfloat Alpha= (yoffset+backgroupheight)/Backgroupheight; [Self.navigationController.navigationBar setbackgroundimage:[self Imagewithcolor:[[uicolor Orangecolor] Colorwithalphacomponent:alpha]] forbarmetrics:uibarmetricsdefault]; Titlelabel.alpha=Alpha; Alpha=Fabs (Alpha); Alpha=fabs (1-Alpha); Alpha=alpha<0.2?0: alpha-0.2; Bgview.alpha=Alpha;}-(UIImage *) Imagewithcolor: (Uicolor *) color{//Description RectangleCGRect rect = CGRectMake (0.0f,0.0f,1.0f,1.0f); //Open Bitmap ContextUigraphicsbeginimagecontext (rect.size); //Get Bitmap ContextCgcontextref context =Uigraphicsgetcurrentcontext (); //populating a context with a color presentationCgcontextsetfillcolorwithcolor (context, [color Cgcolor]); //Render Contextcgcontextfillrect (context, rect); //get a picture from the contextUIImage *theimage =Uigraphicsgetimagefromcurrentimagecontext (); //End ContextUigraphicsendimagecontext (); returntheimage;}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
UITableView Effects of TopView gradients