发现控件设置了没显示的几种情况
有时候,明明frame和数据都设置了,但是控件却没显示出来,总结一下一下几种情况
-
frame的尺寸和位置不对 \ 或者其实传入的是个空值
-
hidden是否为YES
-
控件是否确实已经添加到父控件中
-
alphe 是否小于 0.01 (当透明度设置很低的时候 , 直接等于隐藏了控件)
-
被其他控件挡住了
-
看一看这个控件的父控件前面5种情况有没发生
总结一下 :
像以下情况 : 假如在init方法中设置frame bounds这里的值是0的,所以frame是0的
实际上任何UIView的 任何Init 方法里面的 bounds,frame等值,都是0
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initwithreuseidentifier:reuseidentifier]) {
//创建子控件
UIButton *btn = [uibutton buttonwithtype:uibuttontypecustom];
btn.frame = self.bounds; // 这里的bounds的值是0
[self.contentview addsubview:btn];
UILabel *countlabel = [[uilabel alloc] init];
[self.contentview addsubview:countlabel];
}
return self;
}
所有控件初始化方法里,并不适合设置宽高,所以一般在以下方法中去设置
/**
* 一般重写这个方法,去设置frame
* 当一个控件的frame发生改变时,就会调用(改变一次调用一次)
* 一般在这里布局内部的子控件(设置子控件的frame)
*/
- (void)layoutSubviews
{
//一定要调用父控件的layoutSubviews方法
[super layoutsubviews];
}