iOS入门

01.项目实战 百思不得姐 项目界面框架搭建

2016-01-24  本文已影响1446人  Liwx

@(iOS 项目实战)[项目实战]


目录


1.项目环境搭建

工程项目配置

工程项目配置图

2.项目框架

项目总体框架图,效果图


3.自定义TabBar

自定义TabBar的原因

需在TabBar中间添加按钮,系统的UITabBar不能满足需求.所以使用自定义TabBar

自定义TabBar步骤

#pragma =======================================================================
#pragma mark - 懒加载
// ----------------------------------------------------------------------------
// 加号按钮
- (UIButton *)plusButton
{
    if (_plusButton == nil) {
        UIButton *plusButton = [[UIButton alloc] init];
        [plusButton setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [plusButton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        // 设置尺寸
        [plusButton sizeToFit];
        _plusButton = plusButton;
        
        [self addSubview:plusButton];
    }
    return _plusButton;
}
#pragma =======================================================================
#pragma mark - 布局子控件
// ----------------------------------------------------------------------------
// 重新布局tabBar子控件
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 1.定义frame属性
    NSInteger count = self.items.count;
    CGFloat itemX = 0;
    CGFloat itemY = 0;
    CGFloat itemW = self.wx_width / (count + 1);
    CGFloat itemH = self.wx_height;
    
    // 2.遍历子控件(过滤UITabBarButton), UITabBarButton是私有属性
    NSInteger index = 0;
    for (UIView *view in self.subviews) {
        // 2.1 过滤UITabBarButton
        // 可以用两张方式判断
        // 1.[view isKindOfClass:NSClassFromString(@"UITabBarButton")]
        // 2.[@"UITabBarButton" isEqualToString:NSStringFromClass([view class])]
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            // 2.2 计算x值,并设置frame
            itemX = index * itemW;
            view.frame = CGRectMake(itemX, itemY, itemW, itemH);
            
            index++;
            // 判断如果是是第二个batBarButton,空一格
            if (index == 2) {
                index++;
            }
        }
    }
    
    // 3.设置加号按钮
    self.plusButton.center = CGPointMake(self.wx_width * 0.5, self.wx_height * 0.5);
}

4.自定义TabBarController

设置UIWindow的根控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 1.创建window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    // 2.设置window的根控制器
    self.window.rootViewController = [[WXTabBarController alloc] init];
    
    // 3.让window成为主窗口,并显示
    [self.window makeKeyAndVisible];
    
    return YES;
}

自定义TabBarController步骤

// ----------------------------------------------------------------------------
// 添加所有子控制器
- (void)setupAllChildViewController
{
    // 精华
    [self setupOneChildViewController:[[WXEssenceViewController alloc] init]];
    
    // 新帖
    [self setupOneChildViewController:[[WXNewViewController alloc] init]];
    
    // 发布
//    [self setupOneChildViewController:[[WXPublishViewController alloc] init]];
    
    // 关注
    [self setupOneChildViewController:[[WXFriendTrendViewController alloc] init]];
    
    // 我
    [self setupOneChildViewController:[[WXMeViewController alloc] init]];
}

// ----------------------------------------------------------------------------
// 添加一个子控制器
- (void)setupOneChildViewController:(UIViewController *)viewController
{
    // 1.包装导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
    [self addChildViewController:nav];
}
// ----------------------------------------------------------------------------
// 设置所有tabBarButton
- (void)setupAllTabBarButton
{
    // 精华tabBarItem设置
    [self setupOneTabBarButtonVcIndex:0 titile:@"精华" imageName:@"tabBar_essence_icon" selectedImageName:@"tabBar_essence_click_icon"];
    
    // 新帖tabBarItem设置
    [self setupOneTabBarButtonVcIndex:1 titile:@"新帖" imageName:@"tabBar_new_icon" selectedImageName:@"tabBar_new_click_icon"];
    
    // 关注tabBarItem设置
    [self setupOneTabBarButtonVcIndex:2 titile:@"关注" imageName:@"tabBar_friendTrends_icon" selectedImageName:@"tabBar_friendTrends_click_icon"];
    
    // 我tabBarItem设置
    [self setupOneTabBarButtonVcIndex:3 titile:@"我" imageName:@"tabBar_me_icon" selectedImageName:@"tabBar_me_click_icon"];
}

// ----------------------------------------------------------------------------
// 设置一个tabBarButton信息
- (void)setupOneTabBarButtonVcIndex:(NSInteger)vcIndex titile:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 1.获取子控制器(UINavigationController),并设置标题和图片
    WXNavigationController *nav = self.childViewControllers[vcIndex];
    
    nav.tabBarItem.title = title;
    nav.tabBarItem.image = [UIImage imageNamed:imageName];
    nav.tabBarItem.selectedImage = [UIImage imageWithOriginalRender:selectedImageName];
}
// ----------------------------------------------------------------------------
// 设置自定义tabBar
- (void)setupTabBar
{
    WXTabBar *tabBar = [[WXTabBar alloc] init];
    // 1.将系统的tabBar换成自定义tabBar,系统的tabBar是readOnly,用KVC设置
    [self setValue:tabBar forKey:@"tabBar"];
}
// ----------------------------------------------------------------------------
// 第一次使用当前类或者它的子类的时候调用
+ (void)initialize
{
    // 一次性设置设置tabBarItem字体颜色
    // 1.判断是否是WXTabBarController
    if (self == [WXTabBarController class]) {
        // 1.1 获取item
        UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
        
        NSDictionary *attrNormal = @{NSForegroundColorAttributeName : [UIColor redColor]};
        [item setTitleTextAttributes:attrNormal forState:UIControlStateSelected];
        NSDictionary *attrHigh = @{NSForegroundColorAttributeName : [UIColor grayColor]};
        [item setTitleTextAttributes:attrHigh forState:UIControlStateNormal];
    }
}
上一篇 下一篇

猜你喜欢

热点阅读