iOS UIView contentMode

Content Modes

contentMode是UIView的属性,这个属性的值决定了,当视图的几何形状变化时如何复用它的内容。当视图第一次展示前,它会将自己的内容渲染成一张底层的bitmap. 然后视图的几何变化都不会使bitmap重新生成。而视图contentMode属性的值决定了bitmap是否缩放、位置在哪儿(固定在左边、右边、上面、下面、居中)。默认情况下,contentMode的值是UIViewContentModeScaleToFill。

contentMode啥时候起作用呢?

  • 视图frame或bounds的高宽发生变化
  • 赋给 view的transform属性的值带有scale
UIViewContentModeScaleToFill

Scales the content to fit the size of itself by changing the aspect ratio of the content if necessary.
 改变内容的高宽比例,缩放内容,UIView中完整显示内容,填满UIView,

UIViewContentModeScaleAspectFit
 Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
保持内容的高宽比,缩放内容,完整显示内容,最大化填充UIview,没填充上的区域透明

UIViewContentModeScaleAspectFill
Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.
保持内容高宽比,缩放内容,超出视图的部分内容会被裁减,填充UIView
需要把View的 clipsToBounds 设置为YES;

UIViewContentModeRedraw
The option to redisplay the view when the bounds change by invoking the setNeedsDisplay method.
当View的bounds改变,系统会调用setNeedsDisplay,重新绘制视图

UIViewContentModeCenter
The option to center the content in the view’s bounds, keeping the proportions the same.
不缩放,内容在视图中间

UIViewContentModeTop
The option to center the content aligned at the top in the view’s bounds.

UIViewContentModeBottom
The option to center the content aligned at the bottom in the view’s bounds.

UIViewContentModeLeft
The option to align the content on the left of the view.

UIViewContentModeRight
The option to align the content on the right of the view.

UIViewContentModeTopLeft
The option to align the content in the top-left corner of the view.

UIViewContentModeTopRight
The option to align the content in the top-right corner of the view.

UIViewContentModeBottomLeft
The option to align the content in the bottom-left corner of the view.

UIViewContentModeBottomRight
The option to align the content in the bottom-right corner of the view.

效果图:

image



参考链接stackoverflow

Content Modes

时间: 2024-07-31 09:09:33

iOS UIView contentMode的相关文章

iOS UIView常见属性方法小结_IOS

下面通过实例代码给大家详细介绍了iOS UIView常见属性方法,具体代码如下所示: UIView : UIResponder /** 通过一个frame来初始化一个UI控件 */ - (id)initWithFrame:(CGRect)frame; // YES:能够跟用户进行交互 @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // default is YES // 控件的一

iOS UIView动画详解(Objective-C)

    我在之前的一篇博客中<iOS UIView动画详解(Swift)>讲解了使用Swift来实现UIView类下面提供的多种动画效果,如位置动画.旋转动画.缩放动画.颜色动画.透明度动画等等.为了这个题目的完整性,今天我使用Objective-C来完全重写以上的所有的动画.项目案例已经上传至:https://github.com/chenyufeng1991/iOS-UIView-Animation  中的Animation-OC文件夹下,另一个目录下则是Swift实现的动画. (1)位置

iOS - UIView

前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusEnvironment> @available(iOS 2.0, *) public class UIView : UIResponder, NSCodi

iOS - UIView 动画

1.UIView 动画 核心动画 和 UIView 动画 的区别: 核心动画一切都是假象,并不会真实的改变图层的属性值,如果以后做动画的时候,不需要与用户交互,通常用核心动画(转场). UIView 动画必须通过修改属性的真实值,才有动画效果. 1.1 block 方式 设置控件位置.尺寸.透明度等的代码,放在 animateWithDuration: block 中,将自动以动画的方式改变. // 开始动画,动画持续时间 2 秒 [UIView animateWithDuration:1.0

IOS代码笔记UIView的placeholder的效果_IOS

本文实例为大家分享了IOS占位符效果,供大家参考,具体内容如下 一.效果图   二.工程图   三.代码RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewController <UITextViewDelegate> { UITextView *psTextView; UILabel *pslabel; } @end RootViewController.m #imp

iOS为UIView设置阴影效果_IOS

UIView的阴影设置主要通过UIView的layer的相关属性来设置 阴影的颜色 imgView.layer.shadowColor = [UIColor blackColor].CGColor; 阴影的透明度 imgView.layer.shadowOpacity = 0.8f; 阴影的圆角 imgView.layer.shadowRadius = 4.f; 阴影偏移量 imgView.layer.shadowOffset = CGSizeMake(4,4); imgView.layer.s

ios-IOS多个UIView添加点击

问题描述 IOS多个UIView添加点击 刚刚接触IOS开发,遇到以下问题想不通,希望高手给予指导.我给sendView. payVIew.receiverView. commentView设置点击事件,为什么 UITapGestureRecognizer * singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewClick:)]; singleRecognizer.n

iOS 实现多代理的方法及实例代码_IOS

iOS实现多代理 什么是多代理 用过环信SDK的同学应该对多代理不陌生了,请看下面代码: @method @brief 注册一个监听对象到监听列表中 @discussion 把监听对象添加到监听列表中准备接收相应的事件 @param delegate 需要注册的监听对象 @param queue 通知监听对象时的线程 @result */ - (void)addDelegate:(id<EMChatManagerDelegate>)delegate delegateQueue:(dispatc

iOS基础动画教程分享_IOS

iOS的动画多种多样,动画做的好的应用会更加吸引人,用起来也会更加炫目,本文介绍iOS几种基础动画,单个讲解便于理解,但真正使用时,结合起来用会看起来更加帅,这就看具体的应用场景和大家的想象力啦. 所有的基础动画都给予UIView一个基础的方法:animateWithDuration.这个方法可以包含一个代码块,里面设置要改变的东西,在执行的时候iOS会自动以动画的形式展现出来,代码如下: [UIView animateWithDuration:1 animations:^{ // 要执行的动作