標籤:新浪微博 ios uitabbarcontroller
轉載請標明出處:http://blog.csdn.net/android_ls/article/details/45827719
聲明:仿新浪微博項目,所用所有圖片資源都來源於官方新浪微博IOS用戶端,編寫本應用的目的在於學習交流,如涉及侵權請告知,我會及時換掉用到的相關圖片。
最近我打算利用業餘時間,仿下新浪微博IOS用戶端,至於能寫到哪裡我也不確定,能寫多少就寫多少吧,下面我們開始項目的基本搭建:
1、開啟Xcode選擇建立新項目,並建立各個模組的目錄結構,完成後項目的目錄結構如:
2、我採用的是使用真實的檔案夾組織各個模組的代碼,選中Classes檔案夾,右鍵Show in Finder可以看到如下幾個檔案:
點擊Classes檔案夾進入,看到如下幾個檔案:
通過上面步驟,大家可以看到,用來組織各個模組代碼檔案夾在硬碟中是真實存在的,這樣做的好處是各個模組的代碼劃分清晰,以後某個模組需要修改或者添加功能,直接找到相應的模組操作即可。
3、在Images.xcassets中設定應用表徵圖如:
對應的設定檔(Contents.json)如下:
{ "images" : [ { "idiom" : "iphone", "size" : "29x29", "scale" : "1x" }, { "size" : "29x29", "idiom" : "iphone", "filename" : "[email protected]", "scale" : "2x" }, { "size" : "29x29", "idiom" : "iphone", "filename" : "[email protected]", "scale" : "3x" }, { "size" : "40x40", "idiom" : "iphone", "filename" : "[email protected]", "scale" : "2x" }, { "idiom" : "iphone", "size" : "40x40", "scale" : "3x" }, { "size" : "57x57", "idiom" : "iphone", "filename" : "icon.png", "scale" : "1x" }, { "size" : "57x57", "idiom" : "iphone", "filename" : "[email protected]", "scale" : "2x" }, { "size" : "60x60", "idiom" : "iphone", "filename" : "[email protected]", "scale" : "2x" }, { "size" : "60x60", "idiom" : "iphone", "filename" : "[email protected]", "scale" : "3x" } ], "info" : { "version" : 1, "author" : "xcode" }}
4、設定LaunchImage的圖片如:
想要你的應用在4S、5、5S、6和6plus上顯示都正確,LaunchImage中對應的中的幾張圖片都不可少,每張圖對應的尺寸如下:
Retina HD 5.5 對應的圖片尺寸:1242?×?2208
Retina HD 4.7 對應的圖片尺寸:750?×?1334
iPhone Portrait IOS 7,8(Retina 4) 對應的圖片尺寸:640?×?1136
iPhone Portrait IOS 7,8(2x) 對應的圖片尺寸:640?×?960
比如少了Retina HD 4.7這張圖,如下:左側在LaunchImage中沒有添加Retina HD 4.7這張圖,右側是正常情況下的顯示。
5、建立UIWindow,設定rootViewController並顯示視窗,具體代碼如下:
#pragma mark 應用程式第一次完成啟動,第一個調用的代理方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 1、建立UIWindow self.window = [[UIWindow alloc] init]; self.window.frame = [UIScreen mainScreen].bounds; // 2、設定rootViewController self.window.rootViewController = [[WBTabBarController alloc] init]; // 3、顯示視窗 [self.window makeKeyAndVisible]; return YES;}
6、建立WBTabBarController類,讓其繼承自UITabBarController;依次建立HomeViewController、MessageViewController、DiscoverViewController和ProfileViewController讓它們都繼承自UITableViewController,在WBTabBarController.m檔案中- (void)viewDidLoad {}函數中添加具體實現,代碼如下:
- (void)viewDidLoad { [super viewDidLoad]; _homeViewController = [[HomeViewController alloc]init]; [self addChildController:_homeViewController title:@"首頁" image:@"tabbar_home"]; _messageViewController = [[MessageViewController alloc]init]; [self addChildController:_messageViewController title:@"訊息" image:@"tabbar_message_center"]; _discoverViewController = [[DiscoverViewController alloc]init]; [self addChildController:_discoverViewController title:@"發現" image:@"tabbar_discover"]; _profileViewController = [[ProfileViewController alloc]init]; [self addChildController:_profileViewController title:@"我" image:@"tabbar_profile"];}
在Images.xcassets中,添加相應的圖片資源,添加好後如:
7、WBTabBarController類完整的代碼如下:
//// WBTabBarController.m// SinaWeibo//// Created by android_ls on 15/5/17.// Copyright (c) 2015年 android_ls. All rights reserved.//// 獲得RGB顏色#define kColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]#import "WBTabBarController.h"#import "HomeViewController.h"#import "MessageViewController.h"#import "DiscoverViewController.h"#import "ProfileViewController.h"@interface WBTabBarController (){ HomeViewController * _homeViewController; MessageViewController * _messageViewController; DiscoverViewController * _discoverViewController; ProfileViewController * _profileViewController;}@end@implementation WBTabBarController- (void)viewDidLoad { [super viewDidLoad]; _homeViewController = [[HomeViewController alloc]init]; [self addChildController:_homeViewController title:@"首頁" image:@"tabbar_home"]; _messageViewController = [[MessageViewController alloc]init]; [self addChildController:_messageViewController title:@"訊息" image:@"tabbar_message_center"]; _discoverViewController = [[DiscoverViewController alloc]init]; [self addChildController:_discoverViewController title:@"發現" image:@"tabbar_discover"]; _profileViewController = [[ProfileViewController alloc]init]; [self addChildController:_profileViewController title:@"我" image:@"tabbar_profile"];}/** * 添加子控制器到UITabBarController中 */- (void)addChildController:(UIViewController *)childViewController title:(NSString *)title image:(NSString *)image{ // 設定子控制器,tabbar和navigationBar上的title childViewController.title = title; // 設定tabBarItem上預設的指示圖片和選中時的圖片 childViewController.tabBarItem.image = [UIImage imageNamed:image]; childViewController.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@%@", image, @"_selected"]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // 設定tabBarItem上文字的樣式(這裡是設定文字在不同狀態下的顏色值) [childViewController.tabBarItem setTitleTextAttributes: @{NSForegroundColorAttributeName:kColor(117, 117, 117)} forState:UIControlStateNormal]; [childViewController.tabBarItem setTitleTextAttributes: @{NSForegroundColorAttributeName:kColor(253, 109, 10)} forState:UIControlStateSelected]; [self addChildViewController:[[UINavigationController alloc] initWithRootViewController:childViewController]];}@end
Command + R運行,如下:
就先寫到這裡吧,晚安。
源碼:http://download.csdn.net/detail/android_ls/8714209
仿新浪微博IOS用戶端(v5.2.8)——搭建項目基本架構