自定義UIView UITableViewCell等控件

一、自定義NavigationController導(dǎo)航欄

eg.

CZNavController.h

#import <UIKit/UIKit.h>

@interface CZNavController : UINavigationController

@end

CZNavController.m

#import "CZNavController.h"
#import "UIImage+Ex.h"
@interface CZNavController ()

@end

@implementation CZNavController

//此方法,會在CZNavController當(dāng)前類,執(zhí)行第一個方法之前先會執(zhí)行一次,并且只會調(diào)用一次
+ (void)initialize{
    //設(shè)置導(dǎo)航條的樣式
    UINavigationBar *navBar = [UINavigationBar appearance];
    //UIBarMetricsDefault  背景圖片 在橫豎屏都顯示
    [navBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
    //設(shè)置標(biāo)題的顏色
    [navBar  setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor  whiteColor]}];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
   
}

//重寫導(dǎo)航控制器的push方法,每一個子控制器在跳轉(zhuǎn)的時候都會調(diào)用此方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
//viewController  就是子控制器,設(shè)置子控制器的自定義后退按鈕
    
    //1  自定義后退按鈕
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"NavBack"] originalImage] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    
    //
    UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedItem.width = -10;
    
    viewController.navigationItem.leftBarButtonItems = @[fixedItem,backItem];
    
    //自定義后退按鈕后,手勢返回上一級控制器的功能恢復(fù)
    self.interactivePopGestureRecognizer.delegate = nil;

    //當(dāng)push的時候隱藏tabBar
    viewController.hidesBottomBarWhenPushed = YES;
    
    //真正的做了控制器之間的跳轉(zhuǎn)
    [super pushViewController:viewController animated:animated];
}
- (void)backClick{
    [self popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

三、自定義UIButton

eg.

CZWheelButton.h

#import <UIKit/UIKit.h>

@interface CZWheelButton : UIButton
@property (nonatomic, assign) CGFloat imgW;
@property (nonatomic, assign) CGFloat imgH;

+ (instancetype)wheelButton:(CGFloat)imgW imgH:(CGFloat)imgH;

@end

CZWheelButton.m

#import "CZWheelButton.h"

@implementation CZWheelButton

//設(shè)置imageView的大小和位置
//- (void)layoutSubviews{
//    [super layoutSubviews];
//    
//}

- (void)setHighlighted:(BOOL)highlighted{

}

//快速創(chuàng)建自定義button的對象。并且傳遞按鈕中imageView的大小
+ (instancetype)wheelButton:(CGFloat)imgW imgH:(CGFloat)imgH{
CZWheelButton *btn = [CZWheelButton new];
btn.imgW = imgW;
btn.imgH = imgH;

return btn;
}

//返回按鈕中imageView的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
CGFloat x = (contentRect.size.width - self.imgW) * 0.5;
CGFloat y = 20;

return CGRectMake(x, y, self.imgW, self.imgH);
}

//返回按鈕中titleLabel的frame
//- (CGRect)titleRectForContentRect:(CGRect)contentRect{
//
//}


@end

四、自定義UITableViewCell

eg.

CZItemCell.h

#import <UIKit/UIKit.h>

@class CZItem;
@interface CZItemCell : UITableViewCell
//創(chuàng)建一個可重用的自定義cell
+ (instancetype)cellWithTableView:(UITableView *)tableView;

@property (nonatomic, strong) CZItem *item;
@end

CZItemCell.m

#import "CZItemCell.h"
#import "CZItem.h"
#import "CZItemArrow.h"
#import "CZItemSwitch.h"
@implementation CZItemCell
//1 創(chuàng)建一個可重用的自定義cell
+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *reuseId = @"item";
    CZItemCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    return cell;
}


//2
- (void)setItem:(CZItem *)item{
    _item = item;
    self.textLabel.text = item.title;
    if (item.icon) {
        self.imageView.image = [UIImage imageNamed:item.icon];
    }
    
    //判斷當(dāng)前的模型是箭頭還是開關(guān)
    if ([item isKindOfClass:[CZItemArrow class]]) {
        //設(shè)置箭頭
        //        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        self.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellArrow"]];
    }else if([item isKindOfClass:[CZItemSwitch class]]){
        //不允許cell選中
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryView = [UISwitch new];
    }else{
        
        self.accessoryView = nil;
    }

}
@end


今日最深感悟:
要學(xué)會 提取父類,學(xué)會很好使用繼承

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,527評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,687評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,640評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,957評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,682評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,011評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,009評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,183評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,714評論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,435評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,665評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,148評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,838評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,251評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,588評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,379評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,627評論 2 380

推薦閱讀更多精彩內(nèi)容