隐藏的NavigationBar
导读
今天看了一个关于隐藏NavigationBar的博文,博文中提到了一些值得学习的东西。对于刚刚入行的小白来说,运行时这项功能只闻其声但重来没有使用过。今天看了讲解对于使用运行时来给分来填家属性的功能着实让我大开眼界。
我们知道,使用Category可以很方便地为现有的类增加方法,但却无法直接增加实例变量。不过从Mac OS X v10.6开始,系统提供了Associative References,这个问题就很容易解决了。这种方法也就是所谓的关联(association),我们可以在runtime期间动态地添加任意多的属性,并且随时读取。所用到的两个重要runtime API是:
* objc_setAssociatedObject 添加关系
**
* Sets an associated value for a given object using a given key and association policy.
*
* @param object The source object for the association.
* @param key The key for the association.
* @param value The value to associate with the key key for object. Pass nil to clear an existing association.
* @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
*
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
* objc_getAssociatedObject 取值
* Returns the value associated with a given object for a given key.
*
* @param object The source object for the association.
* @param key The key for the association.
*
* @return The value associated with the key \e key for \e object.
*
* @see objc_setAssociatedObject
OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
使用说明(代码)
一般我们有个原则:能用category扩展就不用继承,因为随着继承深度的增加,代码的可维护性也会增加很多。
现在来看一下分类对外提供的接口
分类接口如下
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@interface UILabel (Associate)//单单从头文件看是不是很像一个类?再看看.m文件你就知道这些都 是假象了
- (nonatomic, retain) UIColor *FlashColor;
@end
分类实现代码
#import "UILabel+Associate.h"
@implementation UILabel (Associate)@dynamic FlashColor;
static char flashColorKey;
- (void) setFlashColor:(UIColor *) flashColor{
objc_setAssociatedObject(self, &flashColorKey, flashColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIColor *) getFlashColor{
return objc_getAssociatedObject(self, &flashColorKey);
}
@end
下面是对隐藏NavigationBar的实现
Other
self.automaticallyAdjustsScrollViewInsets = NO; 设置scrollView不自动偏移
UICollectionView的使用
//创建CollectionView
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];;
//设置item属性
layout.itemSize = CGSizeMake(150, 200);
layout.minimumInteritemSpacing = 20;
layout.sectionInset = UIEdgeInsetsMake(270, 20, 20, 20);
collectionView.backgroundColor = [UIColor whiteColor];
//添加到控制器上
[self.view addSubview:collectionView];
self.collectionView = collectionView;
[self setHeaderView];
collectionView.dataSource = self;
//成为collectionView代理,监听滚动.
collectionView.delegate = self;
//注册cell
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"test"];
pragma mark - 数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"test" forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
return cell;
}