UIViewContentModeScaleAspectFit和UIViewContentModeScaleToFill有什么区别......?
firstButton是Custom类型的UIButton.我以编程方式将三个放在一个表的每个单元格中,因此:
[firstButton setImage:markImage forState:UIControlStateNormal];
[firstButton setContentMode:UIViewContentModeScaleAspectFit];
[cell.contentView addSubview:firstButton];
Run Code Online (Sandbox Code Playgroud)
在其他地方,我告诉它clipToBounds.我得到的是图像中心方块的裁剪,而不是它的纵横比例渲染.我已经尝试了很多方法,包括在firstButton.imageView上设置mode属性,这似乎也不起作用.
我有一个图像,我希望UITableViewCell
使用UITableViewCellStyleSubtitle
提供一个UIImageView
和两行文本的样式,以及一个可选的附件视图显示.
但是,当我设置的内容模式UIImageView
,以任一UIViewContentModeScaleAspectFit
或者UIViewContentModeScaleAspectFill
,它似乎没有什么区别.这两种内容模式都只显示整个图像,将文本向右移动以为图像腾出空间.
有没有人在实践中有任何实际差异的例子?文档说:
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.
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.
Run Code Online (Sandbox Code Playgroud)
但是这些内容模式似乎没有像宣传的那样表现.
编辑:我正在运行SDK iOS 4.
我有一个UIImageView
配置了自动布局的对象.我创建了约束,以便视图与其superview保持恒定的距离.在视觉格式中它将是:
@"V:|-[imageView]-|"
@"H:|-[imageView]-|"
Run Code Online (Sandbox Code Playgroud)
但我还想保留底层图像的宽高比,所以我已经分配UIViewContentModeScaleAspectFit
给了contentMode
.
我认为一切都运行良好,直到我设置相关的cornerRadius
值CALayer
:
self.imageView.layer.cornerRadius = 7;
self.imageView.layer.maskToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
现在,当调整图像视图的大小时,例如由于方向的改变,圆角会根据视图获得的新大小而丢失.原因是cornerRadius
适用于UIImageView
(下面的破折号中的框架),但由于底层图像也调整为尊重contentMode
(下面的星号中的框架),因此圆角不再可见:
--------------------------
| ********** |
| * * |
| * * |
| * * |
| ********** |
--------------------------
Run Code Online (Sandbox Code Playgroud)
有没有办法防止这种行为?
我UIImage
为我的UIButton 设置了一个[ myButton setImage:forState:];
和我设置它contentMode
使用[[myButton imageView] setContentMode:UIViewContentModeScaleAspectFit];
但是当你点击按钮时,它会回到UIViewContentModeScaleToFill
并拉伸我的图像.
使用adjustsImageWhenHighlighted
修复这个,但后来我松了变暗效果,我想保留.
有关如何应对此问题的任何建议?
有没有办法设置UIButton图像,BackgroundImage或ImageView属性内容模式属性?
我尝试过直接的方法(它当然不起作用......):
self.imageButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
Run Code Online (Sandbox Code Playgroud)
有一个带图像的按钮很好,但如果没有办法正确设置它,它不是很方便...
根据UIView关于该contentMode
物业的官方文件:
The content mode specifies how the cached bitmap of the view’s layer is adjusted when the view’s bounds change
Run Code Online (Sandbox Code Playgroud)
什么定义了这个定义中的内容?它是子视图还是我们为视图定义背景颜色时的示例.
我的第一个猜测是它应该至少应用于视图中的子视图,但是例如下面的代码片段在使用UIViewContentModeCenter
标记时不会给我预期的结果 :
UIView* redView = [[UIView alloc] initWithFrame:CGRectMake(80, 80, 150, 200)];
redView.contentMode = UIViewContentModeCenter;
redView.backgroundColor = [UIColor redColor];
UIView* greenView = [[UIView alloc] initWithFrame:redView.bounds];
greenView.backgroundColor = [UIColor greenColor];
[redView addSubview:greenView];
redView.frame = CGRectInset(redView.frame, -5, -5);
[self.view addSubview:redView];
Run Code Online (Sandbox Code Playgroud)
我刚刚设置了一个包含greenView的redView.我还设置了redview的内容模式UIViewContentModeCenter
- 为什么在我编写的代码中,当我更改其父级的框架时,greenView不居中?不应UIViewContentModeCenter
该做什么?
谢谢你的澄清!
Ps:您可以在loadView
简单的视图控制器模板项目中轻松测试上述代码.
我有一个UIImageView
动态设置图像(从URL).图像可以具有任意尺寸/纵横比.我设置contentMode
看点填充和视图的图层clipsToBounds
来YES
,它的正确显示.但是,我还想在图像视图下显示阴影.当我设置阴影图像视图的层上,我需要设置clipsToBounds
到NO
为阴影来显示,这会导致图像的出血部分从要被显示的视图.如何保持图像视图大小不变(纵横填充)并同时启用阴影?
一个选项可能涉及创建图形上下文并将图像重新绘制到该上下文中,获得具有所需宽高比的图像并将该图像设置为我的图像视图的图像,但这是额外的处理/浪费CPU/GPU和普通世界时间(特别是如果我有大量具有大图像的图像视图).
另一个选项可能涉及创建与我的图像视图大小相同的空白视图,将其插入我的超视图中的视图下方,使用约束动态地将其附加到我的视图,以及在该视图的图层上启用阴影.这还涉及为阴影创建额外的视图.可能是比第一个更好/更有效的解决方案,但仍然是额外的工作(CPU方面).
是否有任何选项不需要额外的工作,可以在我的条件下启用阴影和方面拟合?
我正在开发一个iPhone应用程序.我有一个UIImageView,其大小固定为320x480.最初,contentmode设置为AspectFit.当我将内容模式更改为Aspect Fill时,我想显示动画(图像的绘图框架发生变化,UIImageView框架被修复).我该怎么做呢?
谢谢和问候,Deepa
contentmode ×10
ios ×6
uiimageview ×5
uibutton ×3
iphone ×2
uiview ×2
animation ×1
autolayout ×1
calayer ×1
cornerradius ×1
image ×1
ios4 ×1
ios5 ×1
objective-c ×1
scaling ×1
swift ×1
uiimage ×1
xcode ×1