隱藏的NavigationBar

隱藏的NavigationBar

導(dǎo)讀
今天看了一個關(guān)于隱藏NavigationBar的博文,博文中提到了一些值得學(xué)習(xí)的東西。對于剛剛?cè)胄械男“讈碚f,運行時這項功能只聞其聲但重來沒有使用過。今天看了講解對于使用運行時來給分來填家屬性的功能著實讓我大開眼界。
我們知道,使用Category可以很方便地為現(xiàn)有的類增加方法,但卻無法直接增加實例變量。不過從Mac OS X v10.6開始,系統(tǒng)提供了Associative References,這個問題就很容易解決了。這種方法也就是所謂的關(guān)聯(lián)(association),我們可以在runtime期間動態(tài)地添加任意多的屬性,并且隨時讀取。所用到的兩個重要runtime API是:
* objc_setAssociatedObject 添加關(guān)系
    ** 
    * 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擴展就不用繼承,因為隨著繼承深度的增加,代碼的可維護性也會增加很多。
現(xiàn)在來看一下分類對外提供的接口
分類接口如下

#import <UIKit/UIKit.h>
#import <objc/runtime.h>

@interface UILabel (Associate)//單單從頭文件看是不是很像一個類?再看看.m文件你就知道這些都  是假象了
- (nonatomic, retain) UIColor *FlashColor;
@end
分類實現(xiàn)代碼
#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的實現(xiàn)
具體可以參考原文 http://www.itdecent.cn/p/ac237ebcd4fb#

Other

self.automaticallyAdjustsScrollViewInsets = NO;  設(shè)置scrollView不自動偏移
UICollectionView的使用
 //創(chuàng)建CollectionView
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];;

//設(shè)置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代理,監(jiān)聽滾動.
collectionView.delegate = self;
//注冊cell
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"test"];
pragma mark - 數(shù)據(jù)源方法
- (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;
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,041評論 0 9
  • 公司內(nèi)部人員之間的溝通必須要高效,中國文化中稱呼上的繁瑣,在人際關(guān)系處理中增加了很多不必要的麻煩,試想公司內(nèi)部人員...
    曲同寧閱讀 165評論 0 0

友情鏈接更多精彩內(nèi)容