IOS个人项目-微博

一、基本配置以及主体框架

项目主体架构:用tabBarController为根控制器,四个navigationController为它的子控制器,这四个navi管理各自的VC.
项目文件夹类型:用MVC的方式分类,大概为四个模块,在ohter文件夹中放入必要的分类文件跟扩展文件以及常用第三方,创建pch文件,设置路径
拖入启动图片跟appIcon,初始化window,设置根VC为自定义的tabBarVC,创建子控制器时,抽出一个方法

/**
* 初始化子控制器
*/
- (void)setupChildVc:(UIViewController )vc title:(NSString )title image:(NSString )image selectedImage:(NSString )selectedImage

{

// 设置文字和图片
 vc.tabBarItem.title = title;

vc.tabBarItem.image = [UIImage imageNamed:image];

vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];

vc.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(100)/100.0 green:arc4random_uniform(100)/100.0 blue:arc4random_uniform(100)/100.0 alpha:1.0];

// 包装一个导航控制器, 添加导航控制器为tabbarcontroller的子控制器

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

[self addChildViewController:nav];

}

后面带有UI_APPEARANCE_SELECTOR的方法, 都可以通过appearance对象来统一设置

// 通过appearance统一设置所有UITabBarItem的文字属性

NSMutableDictionary *attrs = [NSMutableDictionary dictionary];

attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];

attrs[NSForegroundColorAttributeName] = [UIColor grayColor];



NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];

selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];

selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];


UITabBarItem *item = [UITabBarItem appearance];

[item setTitleTextAttributes:attrs forState:UIControlStateNormal];

[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

由于tabBar中间的按钮特殊性,考虑自定义tabBar的方式(由于tabBar是只读属性,故用kvc自定义)

[self setValue:[[XMGTabBar alloc] init] forKeyPath:@"tabBar"];

在自定义的tabBar文件里面,由于是通过代码创建,只需重写- (instancetype)initWithFrame:(CGRect)frame方法,在此方法里面创建中间的发布按钮,并layoutSubViews方法里面设置坐标

在设置导航栏左边按钮中,发现创建按钮的代码可以抽出来

// 设置导航栏标题
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];

// 设置导航栏左边的按钮
UIButton *tagButton = [UIButton buttonWithType:UIButtonTypeCustom];
[tagButton setBackgroundImage:[UIImage imageNamed:@"MainTagSubIcon"] forState:UIControlStateNormal];
[tagButton setBackgroundImage:[UIImage imageNamed:@"MainTagSubIconClick"] forState:UIControlStateHighlighted];
tagButton.size = tagButton.currentBackgroundImage.size;
[tagButton addTarget:self action:@selector(tagClick) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tagButton];
为UIBarButtonItem写一个分类
@interface UIBarButtonItem (XMGExtension)
+ (instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action;
@end  

    @implementation UIBarButtonItem (XMGExtension)
    + (instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action
    {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    button.size = button.currentBackgroundImage.size;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[self alloc] initWithCustomView:button];
    }
    @end
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值