目录
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 自定义单元格
-
创建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
-
自定义数据源
-
绑定数据
- (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 简介
视图控制器导航
- 使用 storyboard 导航
- 使用 present 导航
- 使用 NavigationController 导航
NavigationController 纯代码实现
-
创建 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];
-
入栈 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]; }
-
出栈
//==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
-
创建
UIScreen *screen = [UIScreen mainScreen]; self.window = [[UIWindow alloc]initWithFrame:screen.bounds]; UITabBarController *tb = [[UITabBarController alloc]init]; self.window.rootViewController = tb; [self.window makeKeyAndVisible];
-
添加
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];
-
效果
UITabBar 和 UITabBarItem
-
UIImage *homeImg = [[UIImage imageNamed:[NSString stringWithFormat:@"home.png"]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; self.tabBarItem.title = @"第一个"; self.tabBarItem.image = homeImg; self.tabBarItem.badgeValue = @"6";