Autolayout
Intrinsic Content Size
UIView等控件重写intrinsicContentSize 方法,可以增加UIView等的内间距。
同时可以不设置宽高,只设置top,left。
纯代码
- (CGSize)intrinsicContentSize { CGSize originalSize = [super intrinsicContentSize]; CGSize size = CGSizeMake(originalSize.width+20, originalSize.height+20); return size; }复制代码
XIB
Instrinsic Size 属性设置为 Placeholder
案例
UIView中添加两个高度不确定的Label。并自动适配两个Label的高度。
Content Hugging Priority 内容高度变大的优先级
用于两个Label其中一个跟随拉伸
Content Compression Resistance 内容高度变小优先级
用于两个Label其中一个跟随压缩
layoutSubviews 调用时机
frame 发生变化时会调用
直接调用setLayoutSubviews
setNeedsLayout ()复制代码
标记此处需要刷新,但是不会立即调用layoutSubviews()
layoutIfNeeded() 复制代码
如果有需要刷新的标记,立即调用layoutSubviews()
如果要立即刷新,先调用view.setNeedsLayout() ,在然后马上调用layoutIfNeeded() 。
xib布局小技巧
设置此处可以在不同机型上使用不同的布局。
UIStackView
给view添加到UIStackView上,可以是的view有流布局的效果。
UITableView
cell的高度计算
手动计算
手动计算所有cell内控件的高度,相加求和。
使用self-satisfied
- (CGSize)systemLayoutSizeFittingSize: (CGSize)targetSize;复制代码
调用上面的方法,控件可以自动计算cell的高度,但是控件的约束要符合self-satisfied标准。
使用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的绑定方法