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
相关推荐
杂雾无尘1 小时前
掌握生死时速:苹果应用加急审核全攻略!
ios·swift·apple
HarderCoder1 小时前
Swift 6.2 中的 `@concurrent`
ios·swift
Digitally3 小时前
如何将文件从 iPhone 传输到 Android(新指南)
android·ios·iphone
YungFan4 小时前
iOS26适配指南之通知
ios·swift
木叶丸5 小时前
跨平台方案该如何选择?
android·前端·ios
我唔知啊5 小时前
OC底层原理二:OC对象的分类(实例对象、类对象、元类对象)
ios·objective-c
泓博6 小时前
KMP(Kotlin Multiplatform)改造(Android/iOS)老项目
android·ios·kotlin
Digitally6 小时前
如何将信息从 iPhone 同步到Mac(完整步骤和示意图)
macos·ios·iphone
大猫会长7 小时前
使用Mac自带的图像捕捉导出 iPhone 相册
ios·iphone
二流小码农10 天前
鸿蒙开发:基于node脚本实现组件化运行
android·ios·harmonyos