iOS 之自定义view

自定义view的两种方式
第一种 纯手写
//创建
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIImageView *imageView = [[UIImageView alloc]init];
self.imageView = imageView;
[self addSubview:imageView];
UILabel *nameLabel = [[UILabel alloc]init];
self.label = nameLabel;
[self addSubview:nameLabel];
}
return self;
}
/**
在这里设置控件的frame 在改方法中一定要调用【super layoutSubviews】方法 主要是设置空间的frame值
加约束
*/
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0, 0, self.bounds.size.width, 80);
self.label.frame = CGRectMake(0, 80, self.bounds.size.width, self.bounds.size.height - 80);
self.label.textAlignment = NSTextAlignmentCenter;
}
//赋值
- (void)creatImageView:(NSString )imageNameStr andNameStr:(NSString )nameStr {
- self.imageView.image = [UIImage imageNamed:imageNameStr];
self.label.text = nameStr;
}
第二种 xib加载
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self = [CustomView CustomView];
self.frame = frame;
}
return self;
}
//xib 加载 从这个犯法中取出view 然后在进行赋值 创建 就就和上面的步骤是一样的
+ (instancetype)CustomView {
return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值