小白实战之玩转微博(第一回)

2017-01-12  本文已影响108人  阿拉斯加的狗
寒假临近,介于现在时间的充裕,本人准备开始新起高仿一个大家最为熟悉也最为娱乐的一个社交平台,新浪微博.本期实战一是对自己以前学习的知识复习,也是进行笔记整理,如有喜欢的朋友请star.也希望也能热爱喜欢开发的小白同学一点帮助.感谢大家.


项目介绍

整体架构分层.png

代码如下展示:


  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    WKTabBarController *tabBarVc = [[WKTabBarController alloc]init];
    self.window.rootViewController = tabBarVc;
    [self.window makeKeyAndVisible];
    return YES;
}
- 自定义TabBar控制器用来添加四个不同的子控制器

- 创建子控制器
    - (void)setUpOneVc {

    WKHomeTableViewController *homeVc = [[WKHomeTableViewController alloc]init];
    [self addOneChildVc:homeVc title:@"首页" image:@"tabbar_home" selectImage:@"tabbar_home_selected"];
    
    WKMessageTableViewController *messageVc = [[WKMessageTableViewController alloc]init];
    [self addOneChildVc:messageVc title:@"消息" image:@"tabbar_message_center" selectImage:@"tabbar_message_center_selected"];
    
    WKDiscoveryTableViewController *disVc = [[WKDiscoveryTableViewController alloc]init];
    [self addOneChildVc:disVc title:@"发现" image:@"tabbar_discover" selectImage:@"tabbar_discover_selected"];
    
    WKMeTableViewController *meVc = [[WKMeTableViewController alloc]init];
    [self addOneChildVc:meVc title:@"我" image:@"tabbar_profile" selectImage:@"tabbar_profile_selected"];
}

      - (void)addOneChildVc: (UIViewController *)childVc title: (NSString *)title image: (NSString *)image selectImage: (NSString *)selectImage{

    childVc.tabBarItem.title = title;
    childVc.navigationItem.title = title;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [childVc.tabBarItem setTitleTextAttributes:dict forState:UIControlStateSelected];
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    UIImage *selectedImage = [UIImage imageNamed:selectImage];
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = selectedImage;
    WKNavigationController *navVc = [[WKNavigationController alloc]initWithRootViewController:childVc];
    [self addChildViewController:navVc];
}
@interface UIBarButtonItem (Extension)
+ (UIBarButtonItem *)itemWithImage: (NSString *)image HightImage: (NSString *)hightImage target:(id)target action:(SEL)action;



+ (UIBarButtonItem *)itemWithImage: (NSString *)image HightImage: (NSString *)hightImage target:(id)target action:(SEL)action {
    
    UIButton *btn = [[UIButton alloc]init];
    [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:hightImage] forState:UIControlStateHighlighted];
    btn.size = btn.currentBackgroundImage.size;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc]initWithCustomView:btn];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {

    if (self.viewControllers.count > 0) {
        
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_back" HightImage:@"navigationbar_back_highlighted" target:self action:@selector(back)];
        
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_more" HightImage:@"navigationbar_more_highlighted" target:self action:@selector(more)];
        
        viewController.hidesBottomBarWhenPushed = YES;
        
}
    [super pushViewController:viewController animated:animated];
}


/**
 这个方法的作用是在加载这个类的时候只调用一次
 */
+ (void)initialize {

    //设置外观对象
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor orangeColor];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    [item setTitleTextAttributes:dict forState:UIControlStateNormal];
    
    NSMutableDictionary *hightDict = [NSMutableDictionary dictionary];
    hightDict[NSForegroundColorAttributeName] = [UIColor redColor];
    hightDict[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    [item setTitleTextAttributes:hightDict forState:UIControlStateHighlighted];

}

本人每次项目代码都会同步到自己的github网站.有想知道源码跟操作的同学可以进行查阅https://github.com/aryehToDog/WKWeibo

感谢!

上一篇 下一篇

猜你喜欢

热点阅读