UIView等控件重寫intrinsicContentSize 方法,能夠增長UIView等的內間距。bash
同時能夠不設置寬高,只設置top,left。佈局
純代碼ui
- (CGSize)intrinsicContentSize
{
CGSize originalSize = [super intrinsicContentSize];
CGSize size = CGSizeMake(originalSize.width+20, originalSize.height+20);
return size;
}複製代碼
XIBspa
Instrinsic Size 屬性設置爲 Placeholder code
案例圖片
UIView中添加兩個高度不肯定的Label。並自動適配兩個Label的高度。ip
Content Hugging Priority 內容高度變大的優先級ci
用於兩個Label其中一個跟隨拉伸資源
Content Compression Resistance 內容高度變小優先級get
用於兩個Label其中一個跟隨壓縮
frame 發生變化時會調用
直接調用setLayoutSubviews
setNeedsLayout ()複製代碼
標記此處須要刷新,可是不會當即調用layoutSubviews()
layoutIfNeeded() 複製代碼
若是有須要刷新的標記,當即調用layoutSubviews()
若是要當即刷新,先調用view.setNeedsLayout() ,在而後立刻調用layoutIfNeeded() 。
設置此處能夠在不一樣機型上使用不一樣的佈局。
UIStackView
給view添加到UIStackView上,能夠是的view有流佈局的效果。
手動計算
手動計算全部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; 複製代碼
避免離屏渲染
+ (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);複製代碼
Blending 在iOS中指混合顏色判斷。不透明的View疊加,系統須要對圖層進行計算。會拖慢速度。