ios开发-UI-自定义Tabbar 图书布局

#import "ViewController.h"
#import "CGTabbar.h"
#import "QHCListView.h"
#import "QHJavaListView.h"
#import "QHOCListView.h"

@interface ViewController ()<CGTabbarDelegate>
@property(nonatomic,strong)NSArray *books;
@property(nonatomic,weak)QHOCListView *ocListView;
@property(nonatomic,weak)QHJavaListView *javaListView;
@property(nonatomic,weak)QHCListView *cListView;

@end

@implementation ViewController

-(QHOCListView *)ocListView
{
    if (_ocListView == nil) {
        QHOCListView *ocListView = [QHOCListView ocListView];
        [self.view addSubview:ocListView];
        _ocListView = ocListView;
    }
    return _ocListView;
}

-(QHJavaListView *)javaListView
{
    if (_javaListView == nil) {
        QHJavaListView *javaListView = [QHJavaListView javaListView];
        [self.view addSubview:javaListView];
        _javaListView = javaListView;
        NSLog(@"+++++++++++");
    }
    return _javaListView;
}

-(QHCListView *)cListView
{
    if (_cListView == nil) {
        QHCListView *cListView = [QHCListView cListView]
        ;
        [self.view addSubview:cListView];
        _cListView = cListView;
    }
    return _cListView;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle]pathForResource:@"books.plist" ofType:nil];
    _books = [NSArray arrayWithContentsOfFile:path];

    //NSLog(@"%@",_books);
    CGTabbar *tabbar = [CGTabbar tabbar];
    [self.view addSubview:tabbar];

    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];

    [self configButton:btn1 andTitile:@"OC" andImageName:@"upomp_button_keyboard3" andSelectedImageName:@"upomp_button_keyboard3_highlighted"];
    [self configButton:btn2 andTitile:@"Java" andImageName:@"upomp_button_keyboard3" andSelectedImageName:@"upomp_button_keyboard3_highlighted"];
    [self configButton:btn3 andTitile:@"C" andImageName:@"upomp_button_keyboard3" andSelectedImageName:@"upomp_button_keyboard3_highlighted"];
    tabbar.delegate = self;
    tabbar.items = @[btn1,btn2,btn3];
     NSLog(@"%@",_books);
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)configButton:(UIButton *)btn andTitile:(NSString *)title andImageName:(NSString *)imageName andSelectedImageName:(NSString *)selectedImageName
{
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];
}
-(void)tabbarItemSelected:(CGTabbar *)tabbar andItem:(UIButton *)item andIndex:(NSInteger)index
{

    if (index==0) {
        [self.view bringSubviewToFront:self.ocListView];
        NSLog(@"--------------");
    }
    else if(index ==1)
    {
        [self.view bringSubviewToFront:self.javaListView];
    }
    else if(index ==2)
    {
        [self.view bringSubviewToFront:self.cListView];
    }

    [self.view bringSubviewToFront:tabbar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#import <UIKit/UIKit.h>

@interface QHOCListView : UIView
+(id)ocListView;
@property(nonatomic,strong)NSArray *books;
@end
#import "QHOCListView.h"

@implementation QHOCListView
+(id)ocListView
{
    return [[self alloc]init];
}

-(void)willMoveToSuperview:(UIView *)newSuperview
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"books.plist" ofType:nil];
    _books = [NSArray arrayWithContentsOfFile:path];

    NSLog(@"%@",_books);

    self.frame = newSuperview.bounds;
    self.backgroundColor = [UIColor redColor];
    int totalColumns = 3;

    // 应用的尺寸
    CGFloat bookW = 90;
    CGFloat bookH = 125;

    // 间隙 = (view的宽度 - 3 * 应用宽度) / 4
    CGFloat marginX = (self.frame.size.width - totalColumns * bookW) / (totalColumns + 1);
    CGFloat marginY = 15;

    // 根据应用个数创建对应的框(index)
    for (int index = 0; index<self.books.count; index++) {
        // 创建
        UIView *bookView = [[UIView alloc] init];
        // 设置背景色
        bookView.backgroundColor = [UIColor cyanColor];

        // 计算框的位置
        // 计算行号和列号
        int row = index / totalColumns;
        int col = index % totalColumns;
        // 计算x和y
        CGFloat bookX = marginX + col * (bookW + marginX);
        CGFloat bookY = 20 + row * (bookH + marginY);
        // 设置frame
        bookView.frame = CGRectMake(bookX, bookY, bookW, bookH);

        //添加框到控制器的view
        [self addSubview:bookView];

        // 添加控件
        // index位置对应的应用信息
        NSDictionary *bookInfo = self.books[index];

        // 添加图片
        //UIImageView *iconView = [[UIImageView alloc] init];
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:[UIImage imageNamed:bookInfo[@"icon"]] forState:UIControlStateNormal];
        // 设置位置
        CGFloat iconW = 60;
        CGFloat iconH = 80;
        CGFloat iconX = (bookW - iconW) * 0.5;
        CGFloat iconY = 0;
        btn.frame = CGRectMake(iconX, iconY, iconW, iconH);
        [bookView addSubview:btn];

        // 添加名字
        UILabel *nameLabel = [[UILabel alloc] init];
        // 设置位置
        CGFloat nameW = bookW;
        CGFloat nameH = 20;
        CGFloat nameX = 0;
        CGFloat nameY = iconY + iconH;
        nameLabel.frame = CGRectMake(nameX, nameY, nameW, nameH);
        // 设置文字
        nameLabel.text = bookInfo[@"name"];
        // 设置字体
        nameLabel.font = [UIFont systemFontOfSize:13];

        // 设置文字居中对齐
        nameLabel.textAlignment = NSTextAlignmentCenter;
        [bookView addSubview:nameLabel];

        // 添加下载按钮
        UIButton *downloadBtn = [[UIButton alloc] init];
        // 设置位置
        CGFloat downloadX = 12;
        CGFloat downloadY = nameY + nameH;
        CGFloat downloadW = bookW - 2 * downloadX;
        CGFloat downloadH = 20;
        downloadBtn.frame = CGRectMake(downloadX, downloadY, downloadW, downloadH);
        // 设置默认的背景

        UIImage *normalImage = [UIImage imageNamed:@"upomp_button_keyboard3"];
        [downloadBtn setBackgroundImage:normalImage forState:UIControlStateNormal];
        // 设置高亮的背景
        UIImage *highImage = [UIImage imageNamed:@"upomp_button_keyboard3_highlighted"];
        [downloadBtn setBackgroundImage:highImage forState:UIControlStateHighlighted];
        // 设置按钮的文字
        [downloadBtn setTitle:@"download" forState:UIControlStateNormal];

        // 设置按钮文字的字体

        downloadBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [bookView addSubview:downloadBtn];
    }

}
@end
#import <UIKit/UIKit.h>

@class CGTabbar;

@protocol CGTabbarDelegate <NSObject>

-(void)tabbarItemSelected:(CGTabbar *)tabbar andItem:(UIButton *)item andIndex:(NSInteger)index;

@end

@interface CGTabbar : UIView
@property(nonatomic,strong)NSArray *items;
@property(nonatomic,assign)id<CGTabbarDelegate>delegate;
+(id)tabbar;
@end
#import "CGTabbar.h"

@implementation CGTabbar

+(id)tabbar
{
    return [[self alloc]init];
}

-(id)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {

    }
    return self;
}
-(void)btnTouch:(UIButton *)btn
{
    if (btn.selected) return;
    for(UIButton *tmp in self.items)
    {
        tmp.selected = NO;
    }
    btn.selected  = YES;
    [_delegate tabbarItemSelected:self andItem:btn andIndex:btn.tag];
}

-(void)setItems:(NSArray *)items
{
    _items  =items;
    CGFloat btnW = self.frame.size.width/items.count;
    for (int i = 0; i < items.count; i++) {
        UIButton *btn = items[i];
        [self addSubview:btn];
        CGFloat btnX = i*btnW;
        CGFloat btnY = 0;
        CGFloat btnH = self.frame.size.height;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [btn addTarget:self action:@selector(btnTouch:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        [self btnTouch:items[0]];
    }
}
-(void)willMoveToSuperview:(UIView *)newSuperview
{
    CGFloat selfW = newSuperview.bounds.size.width;
    CGFloat selfH = 49;
    CGFloat selfX = 0;
    CGFloat selfY = newSuperview.bounds.size.height-selfH;

    self.frame = CGRectMake(selfX, selfY, selfW, selfH);
    self.backgroundColor = [UIColor grayColor];
}

@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 22:43:17

ios开发-UI-自定义Tabbar 图书布局的相关文章

IOS开发-关于自定义TabBar条

今天在做项目的时候,突然有一个模块需要自定义TabBar条. 在平常很多做项目的时候,都没有去自定义过,大部分都是使用系统自带的.今天整理一个自定义TabBar条的步骤. 首先看下我们最终实现的效果: 首先需要继承UItabBar自定义一个自己的tabBar .h #import <UIKit/UIKit.h> @class THTabBar; @protocol THTabBarDelegate <UITabBarDelegate> @optional - (void)tabBa

ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局 一.实现效果 二.使用纯代码自定义一个tableview的步骤 1.新建一个继承自UITableViewCell的类 2.重写initWithStyle:reuseIdentifier:方法 添加所有需要显示的子控件(不需要设置子控件的数据和frame,  子控件要添加到contentView中

iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文件控件tag值操作 数据模型部分: YYtg.h文件 // // YYtg.h // 01-团购数据显示(没有配套的类) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. //

IOS开发UI篇--UITableView的自定义布局==xib布局

利用Xib进行实现 应用场景:像团购网站的列表数据显示,新闻列表显示等(由于该类的显示的数据单元格内容格式相同) (1)主控制器文件,在文件中实现了自己自定义的代理,加载数据, 1 #import "SLViewController.h" 2 #import "SLTgDatas.h" 3 #import "SLTableViewCell.h" 4 #import "SLFooterView.h" 5 #import &quo

IOS开发UI篇--UITableView的自定义布局==纯代码布局

UITableView中除了利用系统的UItableViewCell不能完成需求进行布局时,还可以进行自定义布局: 自定义布局分为两类:(1)利用代码进行创建 (2)利用xib进行实现: 下面对利用代码进行创建分析: 应用场景:像微博,等列表数据展示(由于微博的每个单元格的数据大小不一致,所以得计算每个单元格的大小) 分析:前提是获取列表数据,然后建立每个单元格的模型(建立单元格模型应继承UITableViewCell)复写 - (id)initWithStyle:(UITableViewCel

iOS开发UI基础—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

ios开发UI基础-使用纯代码自定义UItableviewcell实现一个简单的微博界面布局 一.实现效果 二.使用纯代码自定义一个tableview的步骤 1.新建一个继承自UITableViewCell的类 2.重写initWithStyle:reuseIdentifier:方法 添加所有需要显示的子控件(不需要设置子控件的数据和frame,  子控件要添加到contentView中) 进行子控件一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片) 3.提供2个模型 数据模型:

iOS开发UI篇—自定义瀑布流控件(基本实现)

iOS开发UI篇—自定义瀑布流控件(基本实现) 一.基本实现 说明:在View加载的时候,刷新数据. 1.实现代码 YYViewController.m文件 1 // 2 // YYViewController.m 3 // 06-瀑布流 4 // 5 // Created by apple on 14-7-28. 6 // Copyright (c) 2014年 wendingding. All rights reserved. 7 // 8 9 #import "YYViewControll

iOS开发UI篇—自定义瀑布流控件(接口设计)

iOS开发UI篇—自定义瀑布流控件(接口设计) 一.简单说明 1.关于瀑布流 电商应用要展示商品信息通常是通过瀑布流的方式,因为每个商品的展示图片,长度和商都都不太一样. 如果不用瀑布流的话,展示这样的格子数据,还有一种办法是使用九宫格. 但利用九宫格有一个缺点,那就是每个格子的宽高是一样的,如果一定要使用九宫格来展示,那么展示的商品图片可能会变形. 为了保证商品图片能够按照原来的宽高比进行展示,一般采用的是瀑布流的方式. 2.瀑布流的特点: 由很多的格子组成,但是每个格子的宽度和高速都是不确定

iOS开发UI篇—以微博界面为例使用纯代码自定义cell程序编码全过程(一)

iOS开发UI篇-以微博界面为例使用纯代码自定义cell程序编码全过程(一) 一.storyboard的处理 直接让控制器继承uitableview controller,然后在storyboard中把继承自uiviewcontroller的控制器干掉,重新拖一个tableview controller,和主控制器进行连线. 项目结构和plist文件 二.程序逻辑业务的处理 第一步,把配图和plist中拿到项目中,加载plist数据(非png的图片放到spooding files中) 第二步,字