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
相关推荐
键盘敲没电11 小时前
【iOS】KVC
ios·objective-c·xcode
吾吾伊伊,野鸭惊啼11 小时前
2024最新!!!iOS高级面试题,全!(二)
ios
吾吾伊伊,野鸭惊啼11 小时前
2024最新!!!iOS高级面试题,全!(一)
ios
不会敲代码的VanGogh13 小时前
【iOS】——应用启动流程
macos·ios·objective-c·cocoa
Swift社区15 小时前
Apple 新品发布会亮点有哪些 | Swift 周报 issue 61
ios·swiftui·swift
逻辑克16 小时前
使用 MultipeerConnectivity 在 iOS 中实现近场无线数据传输
ios
dnekmihfbnmv21 小时前
好用的电容笔有哪些推荐一下?年度最值得推荐五款电容笔分享!
ios·电脑·ipad·平板
Magnetic_h2 天前
【iOS】单例模式
笔记·学习·ui·ios·单例模式·objective-c
归辞...2 天前
「iOS」——单例模式
ios·单例模式·cocoa
yanling20232 天前
黑神话悟空mac可以玩吗
macos·ios·crossove·crossove24