iOS开发学习之路【UI界面】——UICollectionVIew、UIViewController、UITabBarController

UICollectionVIew

第一个实例

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UICollectionView *collectionView;
@end
static NSString *cid = @"cid";
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //创建CollectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    //注册cell
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cid];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.view addSubview:self.collectionView];
    // Do any additional setup after loading the view.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 500;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cid forIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
    return cell;
}

@end

在这里插入图片描述

UICollectionVIew 代理方法

//选择cell的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    
    NSLog(@"index = %lu",indexPath.row);
}
//取消选择的方法
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
}
//设置单元格大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(170, 120);
}
//设置边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

UICollectionView 自定义单元格

  1. 创建MyCollectionViewCell重写initWithFrame方法

    #import "MyCollectionViewCell.h"
    
    @implementation MyCollectionViewCell
    - (instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if(self){
    //        NSLog(@"init...");
            [self prepareLayout];
        }
        return self;
    }
    
    -(void)prepareLayout{
        self.img = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 130, 80)];
        self.imgnameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 40, 36)];
        self.authorLable = [[UILabel alloc]initWithFrame:CGRectMake(60, 100, 40, 36)];
        self.downloadLable = [[UILabel alloc]initWithFrame:CGRectMake(110, 100, 30, 36)];
        self.imgnameLabel.font = [UIFont fontWithName:@"Arial" size:10];
        self.authorLable.font = [UIFont fontWithName:@"Arial" size:10];
        self.downloadLable.font = [UIFont fontWithName:@"Arial" size:10];
        
        
        [self.contentView addSubview:self.img];
        [self.contentView addSubview:self.imgnameLabel];
        [self.contentView addSubview:self.authorLable];
        [self.contentView addSubview:self.downloadLable];
    }
    @end
    
  2. 自定义数据源

  3. 绑定数据

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return self.dataSource.count;
    }
    
    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
    - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cid forIndexPath:indexPath];
    //    cell.backgroundColor = [UIColor orangeColor];
        ImageInfo *ii = [self.dataSource objectAtIndex:indexPath.row];
        
        cell.img.image = [UIImage imageNamed:ii.imgPath];
        cell.imgnameLabel.text = ii.imgName;
        cell.downloadLable.text = ii.downloadNum;
        cell.authorLable.text = ii.author;
        
        return cell;
    }
    

在这里插入图片描述

UIViewController 简介

​ 视图控制器导航

  1. 使用 storyboard 导航
  2. 使用 present 导航
  3. 使用 NavigationController 导航

NavigationController 纯代码实现

  1. 创建 rootViewController

    UIScreen *sceen = [UIScreen mainScreen];
        self.window = [[UIWindow alloc]initWithFrame:sceen.bounds];
        MyViewController1 *vc1 = [[MyViewController1 alloc]init];
        vc1.view.backgroundColor = [UIColor blueColor];
        
        UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:vc1];
        
        self.window.rootViewController = nc;
        [self.window makeKeyAndVisible];
    
  2. 入栈 pushViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"Go2" forState:UIControlStateNormal];
        btn.frame = CGRectMake(160, 100, 100, 36);
        [btn addTarget:self action:@selector(go2:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
    
    -(IBAction)go2:(id)sender{
        MyViewController2 *vc2 = [[MyViewController2 alloc]init];
        vc2.view.backgroundColor = [UIColor greenColor];
        
        [self.navigationController pushViewController:vc2 animated:YES];
    }
    
  3. 出栈

    //==back
    - (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated; // Returns the popped controller.
    //出栈到指定的Controller 并且返回
    - (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; // Pops view controllers until the one specified is on top. Returns the popped controllers.
    //其他都出栈,只剩下root Controller
    - (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated; // Pops until there's only a single view controller left on the stack. Returns the popped controllers.
    

NavigationController IB实现

在这里插入图片描述

UInavigationbar 和 UINavigationItem

    self.navigationItem.title = @"Title...";//标题
    self.navigationItem.prompt = @"Prompt...";//提示

在这里插入图片描述

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"自定义返回" style:UIBarButtonItemStylePlain target:nil action:nil];

在这里插入图片描述

    self.navigationController.toolbarHidden = NO;

在这里插入图片描述

UITabBarController

  1. 创建

    	UIScreen *screen = [UIScreen mainScreen];
        self.window = [[UIWindow alloc]initWithFrame:screen.bounds];
        
        UITabBarController *tb = [[UITabBarController alloc]init];
        
        self.window.rootViewController = tb;
        [self.window makeKeyAndVisible];
    
  2. 添加

    	MyViewController1 *vc1 = [[MyViewController1 alloc]init];
        vc1.view.backgroundColor = [UIColor redColor];
        vc1.tabBarItem.title = @"首页";
        
        MyViewController1 *vc2 = [[MyViewController1 alloc]init];
        vc2.view.backgroundColor = [UIColor yellowColor];
        vc2.tabBarItem.title = @"广场";
        
        MyViewController1 *vc3 = [[MyViewController1 alloc]init];
        vc3.view.backgroundColor = [UIColor purpleColor];
        vc3.tabBarItem.title = @"朋友";
        
        MyViewController1 *vc4 = [[MyViewController1 alloc]init];
        vc4.view.backgroundColor = [UIColor orangeColor];
        vc4.tabBarItem.title = @"设置";
        
        tb.viewControllers = @[vc1,vc2,vc3,vc4];
    
  3. 效果

在这里插入图片描述

UITabBar 和 UITabBarItem

  1.  	UIImage *homeImg = [[UIImage imageNamed:[NSString stringWithFormat:@"home.png"]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.tabBarItem.title = @"第一个";
        self.tabBarItem.image = homeImg;
        self.tabBarItem.badgeValue = @"6";
    

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值