一、自定義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é)會很好使用繼承