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
    
阅读更多
文章标签: IOS
个人分类: IOS
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

IOS个人项目-微博

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭