自定義UIView UITableViewCell等控制元件

躍然發表於2015-12-09

一、自定義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

二、自定義UIView(xib方式)

eg.

CZWheelView.h

#import <UIKit/UIKit.h>

@interface CZWheelView : UIView
+ (instancetype)wheelView;
@end

CZWheelView.m

#import "CZWheelView.h"
#import "UIView+Ex.h"
@interface CZWheelView ()


//背景圖片
@property (weak, nonatomic) IBOutlet UIImageView *wheelView;

@property (nonatomic, weak) UIButton *preButton;
@end

@implementation CZWheelView

+ (instancetype)wheelView{
    return [[[NSBundle mainBundle] loadNibNamed:@"CZWheelView" owner:nil options:nil] lastObject];
}

//點選開始按鈕
- (IBAction)startClick:(UIButton *)sender {
}

//1 生成12個按鈕
- (void)awakeFromNib{
    int count = 12; //12個按鈕
    for (int i = 0; i < count; i++) {
        //建立按鈕
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.wheelView addSubview:btn];

        //設定按鈕選中的背景圖片
        UIImage *selBackImg = [UIImage imageNamed:@"LuckyRototeSelected"];
        [btn setBackgroundImage:selBackImg forState:UIControlStateSelected];
        //設定按鈕的大小
        btn.width = selBackImg.size.width;
        btn.height = selBackImg.size.height;

        btn.center = self.wheelView.center;
        //旋轉圖片
        btn.layer.anchorPoint = CGPointMake(0.5, 1);
        btn.transform = CGAffineTransformMakeRotation(i * M_PI*2/count);

        //點選按鈕
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        //
        self.wheelView.userInteractionEnabled = YES;
    }
}

- (void)btnClick:(UIButton *)sender{
    self.preButton.selected = NO;
    sender.selected = YES;

    self.preButton = sender;
}

@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

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

相關文章