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
相关推荐
终端安全笔记2 小时前
iOS 27 之后「策略空转」:设备升级不报错,但旧策略不再管它
android·网络·安全·ios·智能手机
9765033355 小时前
iOS 上架 4.3a 被拒【uniapp专讲】
flutter·ios·objective-c·uniapp·swift
黑化旺仔18 小时前
【OC】KVO
macos·ios·objective-c·cocoa
HouWan20 小时前
SwiftUI 小技巧:如何读取 SwiftUI Font 的详细信息
ios·swiftui·apple
李游Leo20 小时前
HarmonyOS 7 实战开发 05:把页面做到可上线状态
ios·harmonyos
终端安全笔记1 天前
描述文件、企业证书、MDM 不是三个名字,是三层
android·ios·智能手机
传奇开心果编程1 天前
【SwiftUI入门练中学】第1课 从零开始
学习·ui·ios·swiftui·swift
丿Wayne1 天前
iOS Token 刷新实战(Actor 避免并发重复刷新)
ios·swift·token·actor·swift并发
终端安全笔记2 天前
iOS 27 给了租赁一个新工具,但它只认受监督的设备
android·网络·安全·ios
大熊猫侯佩2 天前
独立 App 首发完成 iPhone Duo 展开态适配
ios·swiftui·swift