转载请标明出处: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" : "AppIcon29x29@2x.png",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "AppIcon29x29@3x.png",
"scale" : "3x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "AppIcon40x40@2x.png",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"size" : "57x57",
"idiom" : "iphone",
"filename" : "icon.png",
"scale" : "1x"
},
{
"size" : "57x57",
"idiom" : "iphone",
"filename" : "icon@2x.png",
"scale" : "2x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "icon_os7@2x.png",
"scale" : "2x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "icon@3x.png",
"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