大家对于UIViewController的生命周期都相当了解了。但是对于继承UIView的子类能做什么,却很少有文章介绍的。

1.  -initWithFrame:(CGRect)rect是view指定的初始化方法。如果要继承UIView 的初始化就需要直接或间接的调用这个方法。

具体使用如下:

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;

}

注意到在初始化中,做了一些额外的事情 [self createUI];

2. 当你需要调用-(void)layoutSubViews:来布局子视图。一般视图需要重绘时需要会调用layoutSubViews.

layoutSubView都是什么时候会被调用:

• init 时,并不会调用layoutSubviews

• [A addSubView:B]会导致A 和B 以及其所有的subviews的layoutSubviews被调用。

•  view 的setFrame会智能的调用layoutSubviews。会先判断view 的frame是否发生改变,如果改变了就调用layoutSubViews,否则将不会调用。

• scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and its superview  滚动一个UIScrollView会导致在scrollView和它之上的superView的layoutSubViews会被调用。

• 旋转一个设备仅仅会调用viewController对应的父 view 上会调用layoutSubView

• Resizing a view will call layoutSubviews on its superview

重新调整一个view的时候会调用它的superview的layoutSubViews方法

 

- (void)layoutSubviews{

    [super layoutSubviews];

// 背景
    [_backImageView setFrame:self.bounds];

    CGRect rectTitle = CGRectMake(0, 0, self.width, _titleLabel.font.pointSize);

    [_titleLabel setFrame:rectTitle];
    [_textLabel setFrame:rectText];

}

 

注意一定要调用[super layoutSubViews]方法。

3.  - (void)drawRect:(CGRect)rect 用传递给的Rect给接受者
绘制图片

用在哪里:  如果执行平常的绘图操作,就需要用到这个方法。

 

  1. // Only override drawRect: if you perform custom drawing.  
  2. // An empty implementation adversely affects performance during animation.  
  3. - (void)drawRect:(CGRect)rect  
  4. {  
  5.     UIColor *color = [UIColor redColor];  
  6.     [color set]; //设置线条颜色  
  7.       //创建
  8.     UIBezierPath* aPath = [UIBezierPath bezierPath];
  9. //线的属性 设置 
  10.     aPath.lineWidth = 5.0;  
  11.     aPath.lineCapStyle = kCGLineCapRound; //线条拐角  
  12.     aPath.lineJoinStyle = kCGLineCapRound; //终点处理  
  13.       
     
  14.     // Set the starting point of the shape.  
  15.     [aPath moveToPoint:CGPointMake(100.0, 0.0)];  
  16.       
     
  17.     // Draw the lines  
  18.     [aPath addLineToPoint:CGPointMake(200.0, 40.0)];  
  19.     [aPath addLineToPoint:CGPointMake(160, 140)];  
  20.     [aPath addLineToPoint:CGPointMake(40.0, 140)];  
  21.     [aPath addLineToPoint:CGPointMake(0.0, 40.0)];  
  22.     [aPath closePath];//第五条线通过调用closePath方法得到的  
  23.       
     
  24.     [aPath stroke];//Draws line 根据坐标点连线  

何时会被调用:

     当显式的调用[self setNeedDisplay]时会在下个event loop 被调用。或在viewController里调用[self.view setNeedDisplay]也可以。

4. 在重写tabelView cell 时会用到 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 。

使用方法如下:

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        //进行额外的操作

        [self creatUI];
    }
    return self;

}

当然操作的时候要注意尽量在cell 的content view上进行操作。

在tableview cell 里也可以调用UIView指定的初始化方法。

 

 

 

最新文章

  1. 适配iOS 10以及Xcode 8(转载)
  2. Access批量操作
  3. MySQL Batch 与 Transaction
  4. grunt安装
  5. stl-基本知识
  6. java集合框架之java HashMap代码解析
  7. 流行的MySql版本
  8. LeetCode OJ 160. Intersection of Two Linked Lists
  9. JAVA基础:自己构造一个按递增排列的数组,用户输入一个数,插入适当位置
  10. Android使用百度地图定位并显示手机位置后使用前置摄像头“偷拍”
  11. linux系统的磁盘空间限制的两种方法
  12. java碎笔
  13. Aho-Corasick算法学习
  14. html与ios交互方法 WebViewJavascriptBridge
  15. Vue.js——vue-resource
  16. 终极版:Mybatis整合Spring配置
  17. localStorage和sessionStorage数据存储
  18. Docker:网络及数据卷设置 [四]
  19. linx6.7 update openssh to 7.7p1
  20. 神州数码OSPF路由汇总配置

热门文章

  1. <正则吃饺子> :关于前端往后端传递布尔值参数的问题
  2. 爬虫库之BeautifulSoup学习(四)
  3. WordCount作业提交到FileInputFormat类中split切分算法和host选择算法过程源码分析
  4. zz 堆空间与栈空间
  5. python3 封装之property 多态 绑定方法classmethod 与 非绑定方法 staticmethod
  6. jquery操作select(option)的取值,设置和选中
  7. SQL Server服务器连接配置
  8. 201621123016 《Java程序设计》第十一周学习总结
  9. 两个MATLAB在线工具,画图啥的都不用安装了
  10. HDU2594 【KMP】