iOS UIViewContentMode 不同效果图文对比

一. iOS提供了的ContentMode有如下几种

其中默认mode是UIViewContentModeScaleToFill

c 复制代码
typedef NS_ENUM(NSInteger, UIViewContentMode) {
    UIViewContentModeScaleToFill,
    UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
    UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
    UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
    UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
    UIViewContentModeTop,
    UIViewContentModeBottom,
    UIViewContentModeLeft,
    UIViewContentModeRight,
    UIViewContentModeTopLeft,
    UIViewContentModeTopRight,
    UIViewContentModeBottomLeft,
    UIViewContentModeBottomRight,
};

二. 煮几个栗子

UIViewContentModeScaleToFill

特点:
强行把原始图片塞进UIImageView设置的size里;

如果宽高比相同,等比例缩放;

如果宽高比不同,会变形塞入;

UIViewContentModeScaleAspectFit

特点:
等比例缩放图片以适应UIImageView的size,不会裁剪图片

图片的宽度或者高度(至少一个)恰好与视图的宽度或者高度相等;

UIViewContentModeScaleAspectFill

特点:
等比例缩放图片但会超出边界,不会裁剪图片

图片的宽度或者高度(至少一个)恰好与视图的宽度或者高度相等;

UIViewContentModeRedraw

效果同默认态UIViewContentModeScaleToFill,设置没什么特殊效果,而是告诉视图在每次设置或者更改frame的时候自动调用drawRect:方法

UIViewContentModeLeft 和 UIViewContentModeRight


效果为左对齐、右对齐;如上图对比

UIViewContentModeTop 和 UIViewContentModeBottom
UIViewContentModeTopLeft、UIViewContentModeTopRight、UIViewContentModeBottomLeft、UIViewContentModeBottomRight
相关推荐
2501_9159184111 小时前
移动端 HTTPS 抓包实战,多工具组合分析与高效排查指南
数据库·网络协议·ios·小程序·https·uni-app·iphone
Digitally11 小时前
解决“Move to iOS 卡在准备中”的 9 种有效方法
macos·ios·cocoa
5***790015 小时前
Swift进阶
开发语言·ios·swift
denggun1234515 小时前
ios卡顿优化
ios·xcode
從南走到北15 小时前
JAVA国际版打车APP打车顺风车滴滴车跑腿APP源码Android+IOS+H5
android·java·ios
QuantumLeap丶16 小时前
《Flutter全栈开发实战指南:从零到高级》- 13 -状态管理GetX
android·flutter·ios·前端框架
從南走到北20 小时前
JAVA国际版二手车交易二手车市场系统源码支持Android+IOS+H5+APP
android·java·ios
ajassi200021 小时前
开源 Objective-C IOS 应用开发(九)复杂控件-tableview
ios·开源·objective-c
2501_9151063221 小时前
iOS性能调优的系统化实践,从架构分层到多工具协同的全流程优化指南(开发者深度版)
android·ios·小程序·架构·uni-app·iphone·webview
uiop_uiop_uiop21 小时前
iOS arm64e hook MGCopyAnswer got Crash or Only Partial results got hooked
macos·ios·cocoa