對於自定義view,關鍵是思惟,你以爲是怎樣就怎樣,並非只有一種答案。字體
對於上篇博客的第一個自定義view的代碼:atom
#import <UIKit/UIKit.h>spa
@interface InputAlertView : UIView.net
@property (nonatomic , strong) UILabel *changeInfoLabel;3d
@property (nonatomic , strong) UIView *lineView1;orm
@property (nonatomic , strong) UITextField *inputPwdTF;get
@property (nonatomic , strong) UIView *lineView2;input
@property (nonatomic , strong) UIButton *okBtn;博客
@endit
#define kRowHeight (self.frame.size.height-2)/3
#import "InputAlertView.h"
@implementation InputAlertView
- (instancetype) initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self subViews];
[self makeSubviewConstraint];
}
return self;
}
- (void) subViews
{
[self addSubview:self.changeInfoLabel];
[self addSubview:self.lineView1];
[self addSubview:self.inputPwdTF];
[self addSubview:self.lineView2];
[self addSubview:self.okBtn];
}
- (void) makeSubviewConstraint
{
[_changeInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.left.and.right.equalTo(self);
make.height.equalTo(@(kRowHeight+5));
}];
[_lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(self);
make.top.equalTo(_changeInfoLabel.mas_bottom);
make.height.equalTo(@1);
}];
[_inputPwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(self);
make.top.equalTo(_lineView1.mas_bottom);
make.height.equalTo(@(kRowHeight-10));
}];
[_lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(self);
make.top.equalTo(_inputPwdTF.mas_bottom);
make.height.equalTo(@1);
}];
[_okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@35);
make.right.equalTo(@(-35));
make.top.equalTo(_lineView2.mas_bottom).offset(10);
make.bottom.equalTo(self).offset(-10);
}];
}
- (UILabel *) changeInfoLabel
{
if (!_changeInfoLabel) {
_changeInfoLabel = [UILabel new];
_changeInfoLabel.textAlignment = NSTextAlignmentCenter;
_changeInfoLabel.font = MC_16_Font;
_changeInfoLabel.textColor = [UIColor blackColor];
}
return _changeInfoLabel;
}
- (UIView *) lineView1
{
if (!_lineView1) {
_lineView1 = [UIView new];
_lineView1.backgroundColor = [UIColor lightGrayColor];
}
return _lineView1;
}
- (UITextField *) inputPwdTF
{
if (!_inputPwdTF) {
_inputPwdTF = [UITextField new];
_inputPwdTF.textAlignment = NSTextAlignmentCenter;
}
return _inputPwdTF;
}
- (UIView *) lineView2
{
if (!_lineView2) {
_lineView2 = [UIView new];
_lineView2.backgroundColor = [UIColor lightGrayColor];
}
return _lineView2;
}
- (UIButton *) okBtn
{
if (!_okBtn) {
_okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_okBtn.backgroundColor = MCColorWithRGBA(224, 71, 79, 1.0);
_okBtn.layer.cornerRadius = 15;
_okBtn.layer.masksToBounds = YES;
}
return _okBtn;
}
@end
對於第二個就是自定義一個tableview:
#import <UIKit/UIKit.h>
@interface alertTableView : UITableView<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong) NSArray *titleArr;
@end
#import "alertTableView.h"
#import "TitleCenterCell.h"
@implementation alertTableView
- (instancetype) initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
if (self = [super initWithFrame:frame style:style]) {
[self registerClass:[TitleCenterCell class] forCellReuseIdentifier:@"centerCell"];
self.dataSource = self;
self.delegate = self;
}
return self;
}
#pragma mark UITableViewDataSource Delegate
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.titleArr.count;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TitleCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:@"centerCell"];
cell.titleCenterLabel.text = self.titleArr[indexPath.row];
return cell;
}
#pragma mark UITableView Delegate
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50.0;
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.f;
}
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.f;
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.separatorInset = UIEdgeInsetsZero;
[cell setLayoutMargins:UIEdgeInsetsZero];
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
其中自定義了一個UITableViewCell使字體居中,代碼以下:
#import <UIKit/UIKit.h>
@interface TitleCenterCell : UITableViewCell
@property (nonatomic , strong) UILabel *titleCenterLabel;
@end
#import "TitleCenterCell.h"
@implementation TitleCenterCell
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
[self addViews];
[self makeLabelContraint];
}
return self;
}
- (void) addViews
{
[self.contentView addSubview:self.titleCenterLabel];
}
- (void) makeLabelContraint
{
[_titleCenterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
}
- (UILabel *) titleCenterLabel
{
if (!_titleCenterLabel) {
_titleCenterLabel = [UILabel new];
_titleCenterLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleCenterLabel;
}
@end