版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
新浪微博
UI主框架 iOS學(xué)院 李明杰主框架分析TabBar主框架分析分析可得:Window的rootViewController是一個(gè)UITabBarControllerUITabBarController自帶的UITabBar長(zhǎng)得比較矬,因此需要自定義一個(gè)UITabBarController,自己添加一個(gè)新的TabBar新建控制器調(diào)整文件目錄結(jié)構(gòu)刪除Storyboard像新浪微博這種項(xiàng)目是沒(méi)有必要使用Storyboard的,價(jià)值不大,而且目前公司的舊項(xiàng)目不可能有Storyboard,大部分是“代碼+Xib的組合”清空Storyboard設(shè)置將程序啟動(dòng)就加在的主Storyboard設(shè)置清掉,不然運(yùn)行程序會(huì)報(bào)諸如“找不到Storyboard文件的錯(cuò)誤”設(shè)置Window的rootViewController由于沒(méi)有了Storyboard,因此需要在IWAppDelegate中手動(dòng)創(chuàng)建Window,rootViewController也需要手動(dòng)指定-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
//1.創(chuàng)建窗口
self.window=[[UIWindow
alloc]initWithFrame:[UIScreen
mainScreen].bounds];
//2.設(shè)置根控制器
self.window.rootViewController=[[IWTabBarController
alloc]init];
//3.顯示窗口[self.window
makeKeyAndVisible];
return
YES;}程序運(yùn)行效果由于沒(méi)有設(shè)置IWTabBarController的子控制器,因此中間的內(nèi)容是空的,只看到底部有一條白色的UITabBar新建5個(gè)子控制器增加TabBar圖片給UITabBar添加相應(yīng)的圖片資源將整個(gè)文件夾拖進(jìn)去創(chuàng)建4個(gè)子控制器-(void)viewDidLoad{[super
viewDidLoad];
//1.添加所有的子控制器[self
addChildViewControllers];}#pragmamark添加所有的子控制器-(void)addChildViewControllers{
//1.首頁(yè)
IWHomeViewController*home=[[IWHomeViewController
alloc]init];home.tabBarItem.title=@"首頁(yè)";home.tabBarItem.image=[UIImage
imageNamed:@"tabbar_home"];home.tabBarItem.selectedImage=[UIImage
imageNamed:@"tabbar_home_selected"];home.view.backgroundColor=[UIColor
brownColor];
//2.消息
IWMessageViewController*message=[[IWMessageViewController
alloc]init];message.tabBarItem.title=@"消息";message.tabBarItem.image=[UIImage
imageNamed:@"tabbar_message_center"];message.tabBarItem.selectedImage=[UIImage
imageNamed:@"tabbar_message_center_selected"];message.view.backgroundColor=[UIColor
whiteColor];創(chuàng)建4個(gè)子控制器 //3.廣場(chǎng)
IWSquareViewController*square=[[IWSquareViewController
alloc]init];square.tabBarItem.title=@"廣場(chǎng)";square.tabBarItem.image=[UIImage
imageNamed:@"tabbar_discover"];square.tabBarItem.selectedImage=[UIImage
imageNamed:@"tabbar_discover_selected"];square.view.backgroundColor=[UIColor
orangeColor];
//4.我
IWMeViewController*me=[[IWMeViewController
alloc]init];me.tabBarItem.title=@"我";me.tabBarItem.image=[UIImage
imageNamed:@"tabbar_profile"];me.tabBarItem.selectedImage=[UIImage
imageNamed:@"tabbar_profile_selected"];me.view.backgroundColor=[UIColor
redColor];
self.viewControllers=@[home,message,square,me];}程序運(yùn)行效果設(shè)置了4個(gè)子控制器,并且每個(gè)控制器都有自己的tabbar標(biāo)題和圖標(biāo)最中間的+號(hào)比較特殊,后面再考慮UI架構(gòu)目前情況下的UI架構(gòu)如下圖所示:一個(gè)IWTabBarController擁有4個(gè)子控制器IWTabBarControllerviewControllerschildViewControllersIWHomeViewControllerIWMessageViewControllerIWMeViewControllerIWSquareViewController在PCH文件中添加常用的宏//1.日志開(kāi)關(guān)#ifdefDEBUG#defineIWLog(...)NSLog(__VA_ARGS__)#else#defineIWLog(...)#endif//2.判斷是否為iOS7#defineiOS7([[[UIDevicecurrentDevice]systemVersion]floatValue]>=7.0)//3.獲得顏色#defineIWColor(r,g,b)[UIColorcolorWithRed:(r)/255.0green:(g)/255.0blue:(b)/255.0alpha:1]小小的代碼重構(gòu)#pragmamark初始化子控制器的屬性-(void)addChildViewController:(UIViewController*)childtitle:(NSString*)titleimage:(NSString*)imageselectedImage:(NSString*)selectedImage{child.tabBarItem.title=title;child.tabBarItem.image=[UIImage
imageNamed:image];child.tabBarItem.selectedImage=[UIImage
imageNamed:selectedImage];
//背景色隨機(jī)
child.view.backgroundColor=IWColor(arc4random_uniform(255),arc4random_uniform(255),arc4random_uniform(255));
[self
addChildViewController:child];}小小的代碼重構(gòu)#pragmamark添加所有的子控制器-(void)addChildViewControllers{
//1.首頁(yè)
IWHomeViewController*home=[[IWHomeViewController
alloc]init];[self
addChildViewController:hometitle:@"首頁(yè)"
image:@"tabbar_home"
selectedImage:@"tabbar_home_selected"];
//2.消息
IWMessageViewController*message=[[IWMessageViewController
alloc]init];[self
addChildViewController:messagetitle:@"消息"
image:@"tabbar_message_center"
selectedImage:@"tabbar_message_center_selected"];小小的代碼重構(gòu)
//3.廣場(chǎng)
IWSquareViewController*square=[[IWSquareViewController
alloc]init];[self
addChildViewController:squaretitle:@"廣場(chǎng)"
image:@"tabbar_discover"
selectedImage:@"tabbar_discover_selected"];
//4.我
IWMeViewController*me=[[IWMeViewController
alloc]init];[self
addChildViewController:metitle:@"我"
image:@"tabbar_profile"
selectedImage:@"tabbar_profile_selected"];}如何添加導(dǎo)航功能目前雖然能隨意切換4個(gè)不同的控制器界面了,但是缺少導(dǎo)航功能,無(wú)法導(dǎo)航切換到下一個(gè)界面現(xiàn)在的4個(gè)界面中,每個(gè)界面都有自己的導(dǎo)航欄內(nèi)容,所以可以考慮給這4個(gè)控制器分別包裝一個(gè)導(dǎo)航控制器首頁(yè)消息廣場(chǎng)包裝導(dǎo)航控制器#pragmamark初始化子控制器的屬性-(void)addChildViewController:(UIViewController*)childtitle:(NSString*)titleimage:(NSString*)imageselectedImage:(NSString*)selectedImage{
//1.設(shè)置tabBarItem屬性child.tabBarItem.title=title;child.tabBarItem.image=[UIImage
imageNamed:image];child.tabBarItem.selectedImage=[UIImage
imageNamed:selectedImage];
child.view.backgroundColor=IWColor(arc4random_uniform(255),arc4random_uniform(255),arc4random_uniform(255));
//2.設(shè)置標(biāo)題和包裝導(dǎo)航控制器
child.title=title;
UINavigationController*nav=[[UINavigationController
alloc]initWithRootViewController:child];
[self
addChildViewController:nav];}運(yùn)行效果UI架構(gòu)目前情況下的UI架構(gòu)如下圖所示:一個(gè)IWTabBarController擁有4個(gè)導(dǎo)航控制器作為子控制器,每個(gè)導(dǎo)航控制器都有自己的根控制器(棧底控制器)IWTabBarControllerviewControllerschildViewControllersUINavigationControllerrootViewControllerUINavigationControllerrootViewControllerUINavigationControllerrootViewControllerUINavigationControllerrootViewControllerIWHomeViewControllerIWMessageViewControllerIWDiscoverViewControllerIWProfileViewController測(cè)試導(dǎo)航功能為了測(cè)試跳轉(zhuǎn),給首頁(yè)增加了20行數(shù)據(jù)-(void)viewDidLoad{[super
viewDidLoad];[self.tableView
registerClass:[UITableViewCell
class]forCellReuseIdentifier:@"Cell"];}#pragmamark-Tableviewdatasource-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{
return
20;}測(cè)試導(dǎo)航功能點(diǎn)擊cell時(shí)跳轉(zhuǎn)到下一個(gè)界面-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{
static
NSString*CellIdentifier=@"Cell";
UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:CellIdentifierforIndexPath:indexPath];cell.textLabel.text=@"測(cè)試數(shù)據(jù)";
returncell;}-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{
UIViewController*vc=[[UIViewController
alloc]init];
vc.title
=@"測(cè)試用的控制器"; vc.view.backgroundColor=[UIColor
yellowColor];[self.navigationController
pushViewController:vcanimated:YES];}測(cè)試的導(dǎo)航效果如下所示由于底部的TabBar不屬于導(dǎo)航控制器的一部分,所以TabBar還留在原地TabBar不消失的解決方案在導(dǎo)航過(guò)程中,TabBar不消失的解決方案是:壓入一個(gè)新的控制器時(shí),設(shè)置控制器的hidesBottomBarWhenPushed為YES即可這個(gè)屬性設(shè)置為YES的作用是:只要上層控制器(父控制器)存在底部工具條,就會(huì)在導(dǎo)航過(guò)程中自動(dòng)隱藏UIViewController*vc=[[UIViewController
alloc]init];vc.title
=@"測(cè)試用的控制器";vc.view.backgroundColor=[UIColor
yellowColor];vc.hidesBottomBarWhenPushed=YES;[self.navigationController
pushViewController:vcanimated:YES];完善后的導(dǎo)航效果如下所示這次的導(dǎo)航效果中,TabBar會(huì)自動(dòng)隱藏自定義導(dǎo)航控制器為了保證在push讓所有的控制器時(shí),都隱藏TabBar,最簡(jiǎn)單有效的做法就是自定義UINavigationController,重寫push方法-(void)pushViewController:(UIViewController*)viewControlleranimated:(BOOL)animated{
//當(dāng)不是壓入的不是棧底控制器,就隱藏底部的工具條
if(self.viewControllers.count){viewController.hidesBottomBarWhenPushed=
YES;}[super
pushViewController:viewControlleranimated:animated];}IWTabBarController-修改導(dǎo)航控制器的類名#pragmamark初始化子控制器的屬性-(void)addChildViewController:(UIViewController*)childtitle:(NSString*)titleimage:(NSString*)imageselectedImage:(NSString*)selectedImage{
//1.設(shè)置tabBarItem屬性child.tabBarItem.title=title;child.tabBarItem.image=[UIImage
imageNamed:image];child.tabBarItem.selectedImage=[UIImage
imageNamed:selectedImage];
child.view.backgroundColor=IWColor(arc4random_uniform(255),arc4random_uniform(255),arc4random_uniform(255));
//2.設(shè)置標(biāo)題和包裝導(dǎo)航控制器child.title=title;
IWNavigationController*nav=[[IWNavigationController
alloc]initWithRootViewController:child];
[self
addChildViewController:nav];}iOS系統(tǒng)占有率目前開(kāi)發(fā)中,主要的適配工作是:iOS6和iOS7的適配iOS6\7圖片適配在新浪提供的UI資源中,分別為iOS6、iOS7準(zhǔn)備了2套不同的圖片比如下面的圖片(左iOS6、右iOS7)從圖片命名規(guī)則上可以看出,iOS7的圖片多了個(gè)_os7的后綴由于整個(gè)項(xiàng)目中的絕大部分圖片都是分iOS6、iOS7版本的,為了同時(shí)適配兩個(gè)系統(tǒng),并且減少加載圖片時(shí)的系統(tǒng)判斷代碼,應(yīng)該給UIImage增加一個(gè)分類,專門負(fù)責(zé)加載當(dāng)前系統(tǒng)對(duì)應(yīng)的圖片,屏蔽判斷系統(tǒng)版本的細(xì)節(jié)iOS6\7圖片適配為了適配iOS6\7的圖片,增加了2個(gè)分類分類NSString+IW.h@interfaceNSString(File)//在文件名后拼接一段字符串(擴(kuò)展名不變)-(NSString*)fileNameAppendString:(NSString*)str;@end分類NSString+IW.m@implementationNSString(File)//在文件名后拼接一段字符串(擴(kuò)展名不變)-(NSString*)fileNameAppendString:(NSString*)str{
//如果沒(méi)有傳入任何字符串
if(str.length==0)return
self;
//1.文件拓展名
NSString*extension=[self
pathExtension];
//2.獲得沒(méi)有拓展名的文件名
NSString*shortName=[self
stringByDeletingPathExtension];
//3.拼接str
NSString*dest=[shortNamestringByAppendingString:str];
//4.拼接拓展名
return[deststringByAppendingPathExtension:extension];}@end分類UIImage+IW.h@interfaceUIImage(IW)+(UIImage*)imag
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 垃圾清運(yùn)與環(huán)保運(yùn)營(yíng)合作合作合同
- 愛(ài)情誓言保證書范文
- 城市交通標(biāo)線工程招標(biāo)
- 建筑材料燒結(jié)磚采購(gòu)
- 確保工程安全勞務(wù)分包合同的重要性
- 內(nèi)墻涂料施工分包合同
- 租賃倉(cāng)庫(kù)合同文本
- 松樹(shù)采購(gòu)合同范本模板
- 銷售水泵合同
- 社會(huì)服務(wù)志愿者契約
- 人工智能如何改進(jìn)學(xué)生的數(shù)學(xué)思維和解題能力
- 《經(jīng)濟(jì)學(xué)方法論》課件
- 土石方工程危險(xiǎn)源辨識(shí)結(jié)果列表
- 兒科急診管理制度
- 術(shù)中低體溫的預(yù)防-
- 醫(yī)師多點(diǎn)執(zhí)業(yè)備案表-申請(qǐng)表-協(xié)議書-情況登記表
- 2025屆高三英語(yǔ)高效復(fù)習(xí)備考策略
- 寫作《寫景如在眼前》(教學(xué)課件)【中職專用】高一語(yǔ)文(高教版2023基礎(chǔ)模塊上冊(cè))
- 認(rèn)知行為療法(CBT)實(shí)操講座
- 走出炭子沖讀后感
- 化工專業(yè)人才創(chuàng)新能力培養(yǎng)模式的探索與實(shí)踐
評(píng)論
0/150
提交評(píng)論