一、自定义NavigationController导航栏
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当前类,执行第一个方法之前先会执行一次,并且只会调用一次
+ (void)initialize{
//设置导航条的样式
UINavigationBar *navBar = [UINavigationBar appearance];
//UIBarMetricsDefault 背景图片 在横竖屏都显示
[navBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
//设置标题的颜色
[navBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
//重写导航控制器的push方法,每一个子控制器在跳转的时候都会调用此方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
//viewController 就是子控制器,设置子控制器的自定义后退按钮
//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];
//自定义后退按钮后,手势返回上一级控制器的功能恢复
self.interactivePopGestureRecognizer.delegate = nil;
//当push的时候隐藏tabBar
viewController.hidesBottomBarWhenPushed = YES;
//真正的做了控制器之间的跳转
[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
//设置imageView的大小和位置
//- (void)layoutSubviews{
// [super layoutSubviews];
//
//}
- (void)setHighlighted:(BOOL)highlighted{
}
//快速创建自定义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
//创建一个可重用的自定义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 创建一个可重用的自定义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];
}
//判断当前的模型是箭头还是开关
if ([item isKindOfClass:[CZItemArrow class]]) {
//设置箭头
// 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
今日最深感悟:
要学会 提取父类,学会很好使用继承