iOS: 仿新浪微博 OC (持续更新ing)

这篇博客详细介绍了如何在iOS应用中实现新浪微博的界面和功能。从配置AppIcon和LaunchImage开始,接着构建项目框架,包括使用自定义的UITabBarController作为窗口的rootViewController。然后,博主展示了如何自定义tabBar,添加撰写按钮并处理点击事件。最后,讨论了实现发现页面,包括城市模型数据的导入和搜索功能的实现。
摘要由CSDN通过智能技术生成

效果图(暂定)
这里写图片描述

1.配置AppIcon和LaunchImage

这里写图片描述

这里写图片描述

2.项目框架初建

需求:
多视图控制器

思路:
(1) 自定义一个继承UITabBarController的类作为window的rootViewController
(2) 给标签控制器上的每个子控制器包上一个根控制器

WBTabBarController.m


- (void)viewDidLoad {
    [super viewDidLoad];

    UITableViewController *homeVC = [self creatChildControllerWithClassName:@"WBHomeTableViewController" andTitle:@"首页" andImageName:@"tabbar_home"];
    UITableViewController *findVC = [self creatChildControllerWithClassName:@"WBFindTableViewController" andTitle:@"发现" andImageName:@"tabbar_discover"];
    UITableViewController *messageVC = [self creatChildControllerWithClassName:@"WBMessageTableViewController" andTitle:@"消息" andImageName:@"tabbar_message_center"];
    UITableViewController *mineVC = [self creatChildControllerWithClassName:@"WBMineTableViewController" andTitle:@"我" andImageName:@"tabbar_profile"];

    self.viewControllers = @[homeVC,messageVC,findVC,mineVC];
    self.tabBar.tintColor = [UIColor orangeColor];

    // KVC替换系统的tabBar
    WBTabBar *tabBar = [[WBTabBar alloc] init];
    tabBar.delegate =self;
    [self setValue:tabBar forKey:@"tabBar"];
}


/**
 创建标签栏子控制器

 @param className 类名
 @param title 标签标题
 @param imageName 图标名称
 @return 子控制器
 */
- (UITableViewController *)creatChildControllerWithClassName:(NSString *)className andTitle: (NSString *)title andImageName: (NSString *)imageName{

    Class cla = NSClassFromString(className);

    UITableViewController *vc = [[cla alloc] init];

    // 标签名
    vc.tabBarItem.title = title;

    // 文字样式
    NSMutableDictionary *titleAttrs = [NSMutableDictionary dictionary];
    titleAttrs[NSForegroundColorAttributeName] = HWColor(123, 123, 123);
    NSMutableDictionary *selectorTitleAttrs = [NSMutableDictionary dictionary];
    selectorTitleAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];

    [vc.tabBarItem setTitleTextAttributes:titleAttrs forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:selectorTitleAttrs forState:UIControlStateSelected];

    // 图标
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    NSString *selectedImageName = [imageName stringByAppendingString:@"_selected"];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    WBNavigationController *nav = [[WBNavigationController alloc] initWithRootViewController:vc];

    [self addChildViewController:nav];

    return nav;

}

WBNavigationController.m
这里用到一个单独抽取的UIBarButtonItem+Extension类,后面有写


+(void)initialize{

    // 设置统一的item样式
    UIBarButtonItem *item = [UIBarButtonItem appearance];

    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];

    // 设置不可用状态
    NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
    disableTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.7];
    disableTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];

}


-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{

    if (self.viewControllers.count > 0) {
        // 跳转后不是子控制器,则自动隐藏tabbar
        viewController.hidesBottomBarWhenPushed = YES;
        /* 设置导航栏上面的内容 */
        // 设置左边的返回按钮
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_back" andTarget:self andSelector:@selector(back)];

        // 设置右边的更多按钮
        viewController.navigationItem
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值