一、自定义构造方法
有时候需要快速创建对象,可以自定义构造方法
+ (instancetype)shopView
{
return [[self alloc] init];
} - (instancetype)initWithShop:(NJShop *)shop
{
if (self = [super init]) {
self.shop = shop;
}
return self;
} + (instancetype)shopViewWithShop:(NJShop *)shop
{ return [[self alloc] initWithShop:shop];
}
一般情况下自定义一个控件会重写控件的initWithFrame方法, , 因为用户可能通过init方法创建也可能通过initWithFrame方法创建, 为了保证无论用户通过哪一个方法创建都能添加子控件, 所以重写initWithFrame
init方法内部会调用initWithFrame
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) { NSLog(@"%s", __func__);
// 1.创建一张图片
UIImageView *iv = [[UIImageView alloc] init];
iv.backgroundColor = [UIColor yellowColor];
[self addSubview:iv];
self.iv = iv; // 2.创建一个文本
UILabel *lable = [[UILabel alloc] init];
lable.backgroundColor = [UIColor purpleColor];
lable.textAlignment = NSTextAlignmentCenter;
[self addSubview:lable];
self.lable = lable;
}
return self;
}
NJShopView *shopView = [[NJShopView alloc] initWithFrame:CGRectMake(shopX, shopY, , )]; NJShopView *shopView = [NJShopView shopView]; NJShopView *shopView = [[NJShopView alloc] initWithShop:self.shops[index]]; NJShopView *shopView = [NJShopView shopViewWithShop: self.shops[index]];
二、layoutSubviews
layoutSubviews方法是专门用于布局子控件的位置的
注意: 重写layoutSubviews方法, 一定要调用[super layoutSubviews]方法 \
如果不调用, 会出现一些奇葩的错误
/*
layoutSubviews方法什么时候调用
1.只要创建一个控件, 那么就会调用
2.只要修改控件的尺寸就会调用(bounds/frame)
3.修改位置不会调用
4.如果当前修改的尺寸和上一次的尺寸没有变化, 不会调用
*/
- (void)layoutSubviews { [super layoutSubviews]; NSLog(@"%s", __func__); CGFloat shopViewWidth = self.frame.size.width; CGFloat shopViewHeight = self.frame.size.height; // 1.布局图片的位置 self.iv.frame = CGRectMake(, , shopViewWidth, shopViewWidth); // 2.布局文本的位置 self.lable.frame = CGRectMake(, shopViewWidth, shopViewWidth, shopViewHeight - self.iv.frame.size.height); }
三、Xib文件
什么是Xib?
Xib和Storyboard一样都是用来描述界面的
Xib是Storyboard的前身
Xib是用于描述一个简单的界面或者一个局部的界面
Storyboard是用于描述整体
四、利用Xib自定义View
#import <UIKit/UIKit.h> @class NJShop;
@interface XMGShopView : UIView @property(nonatomic, strong)NJShop *shop; + (instancetype)shopView;
@end #import "XMGShopView.h"
#import "NJShop.h" @interface XMGShopView ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@end @implementation XMGShopView + (instancetype)shopView
{
return [[[NSBundle mainBundle] loadNibNamed:@"XMGShopView" owner:nil options:nil] firstObject];
} - (void)setShop:(NJShop *)shop
{
_shop = shop; self.iconView.image = [UIImage imageNamed:_shop.icon];
self.nameLabel.text = _shop.name;
} @end
xib加载原理:
// 1. 根据custom class创建对象 XMGShopView *shopView = [XMGShopView alloc] init]; // 2. 根据xib中的设置, 设置控件的相关属性 shopView.backgroundColor = [UIColor redColor]; shopView.frame = CGRectMake(, , , ); // 3. 创建所有子控件, 并且设置子控件的属性 UIImageView *iv = [[UIImageView alloc] init]; iv.frame = CGRectMake(, , , ); UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(, , , ); // 4. 检查子控件是否有连线, 如果有就进行关联 self.iconView = iv; self.nameLabel = label; // 5.将所有子控件添加到父控件中 [shopView addSubview:iv]; [shopView addSubview:label];