《iOS进阶指南》读书笔记

Autolayout

Intrinsic Content Size

UIView等控件重写intrinsicContentSize 方法,能够增长UIView等的内间距。bash

同时能够不设置宽高,只设置top,left。布局

  1. 纯代码ui

    - (CGSize)intrinsicContentSize
      {
        CGSize originalSize = [super intrinsicContentSize];
        CGSize size = CGSizeMake(originalSize.width+20, originalSize.height+20); 
        return size;
      }复制代码
  2. XIBspa

    Instrinsic Size 属性设置为 Placeholder code

  3. 案例图片

    UIView中添加两个高度不肯定的Label。并自动适配两个Label的高度。ip

  4. Content Hugging Priority 内容高度变大的优先级ci

    用于两个Label其中一个跟随拉伸资源

  5. Content Compression Resistance 内容高度变小优先级get

    用于两个Label其中一个跟随压缩

layoutSubviews 调用时机
  1. frame 发生变化时会调用

  2. 直接调用setLayoutSubviews

  3. setNeedsLayout ()复制代码

    标记此处须要刷新,可是不会当即调用layoutSubviews()

  4. layoutIfNeeded()  复制代码

    若是有须要刷新的标记,当即调用layoutSubviews()

  5. 若是要当即刷新,先调用view.setNeedsLayout() ,在而后立刻调用layoutIfNeeded() 。

xib布局小技巧
  1. 设置此处能够在不一样机型上使用不一样的布局。

  2. UIStackView

给view添加到UIStackView上,能够是的view有流布局的效果。

UITableView

cell的高度计算
  1. 手动计算

    手动计算全部cell内控件的高度,相加求和。

  2. 使用self-satisfied

    - (CGSize)systemLayoutSizeFittingSize: (CGSize)targetSize;复制代码

    调用上面的方法,控件能够自动计算cell的高度,可是控件的约束要符合self-satisfied标准。

  3. 使用self-sizing , 此方法会很慢。

    tableView.estimatedRowHeight = 44.0
    tableView.rowHeight = UITableViewAutomaticDimension复制代码

避免离屏渲染

设置圆角

离屏渲染

self.contentView.layer.masksToBounds = YES; 
self.contentView.layer.cornerRadius = 4; 复制代码
使用UIImageView装载一个圆角图片来处理

避免离屏渲染

+ (UIImage*) imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    NSData* imageData = UIImageJPEGRepresentation(image, 1.0f);
    image = [UIImage imageWithData:imageData];
    return image;
}复制代码
- (UIImage *)imageByRoundCornerRadius:(CGFloat)radius
                              corners:(UIRectCorner)corners
                          borderWidth:(CGFloat)borderWidth
                          borderColor:(UIColor *)borderColor
                       borderLineJoin:(CGLineJoin)borderLineJoin {
    
    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextScaleCTM(context, 1, -1);
    CGContextTranslateCTM(context, 0, -rect.size.height);
    
    CGFloat minSize = MIN(self.size.width, self.size.height);
    if (borderWidth < minSize / 2) {
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, borderWidth, borderWidth) byRoundingCorners:corners cornerRadii:CGSizeMake(radius, borderWidth)];
        [path closePath];
        
        CGContextSaveGState(context);
        [path addClip];
        CGContextDrawImage(context, rect, self.CGImage);
        CGContextRestoreGState(context);
    }
    
    if (borderColor && borderWidth < minSize / 2 && borderWidth > 0) {
        CGFloat strokeInset = (floor(borderWidth * self.scale) + 0.5) / self.scale;
        CGRect strokeRect = CGRectInset(rect, strokeInset, strokeInset);
        CGFloat strokeRadius = radius > self.scale / 2 ? radius - self.scale / 2 : 0;
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:strokeRect byRoundingCorners:corners cornerRadii:CGSizeMake(strokeRadius, borderWidth)];
        [path closePath];
        
        path.lineWidth = borderWidth;
        path.lineJoinStyle = borderLineJoin;
        [borderColor setStroke];
        [path stroke];
    }
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}复制代码
设置阴影

离屏渲染

CALayer *shadowLayer = [CALayer layer]; 
shadowLayer = [UIColor blackColor].CGColor; 
shadowLayer.shadowOpacity = 1.0; 
shadowLayer.shadowRadius = 4.0; 
shadowLayer.shadowOffset = CGSizeMake(4.0, 4.0);复制代码

避免离屏渲染

shadowLayer.shadowPath = CGPathCreateWithRect(shadowLayer.bound
  s, NULL);复制代码

尽可能让View不透明

Blending 在iOS中指混合颜色判断。不透明的View叠加,系统须要对图层进行计算。会拖慢速度。

UITableView的解耦

  • 使用plist文件存储静态资源
  • 动态Cell的绑定方法
相关文章
相关标签/搜索