ios - 具有 contentMode UIViewContentModeScaleAspectFit 的 UIImageView 的 UILabel

标签 ios contentmode

我需要在 UIImageView 的顶部设置一个具有自定义背景颜色的标签。问题是我希望它的宽度与 ImageView 中的图像完全一样。这是到目前为止的代码:

UIImageView *pictureV = [[UIImageView alloc] initWithFrame:CGRectMake(pictureX, 0, pictureSize.width, pictureSize.height)];
pictureV.image = self.picture.image;
pictureV.contentMode = UIViewContentModeScaleAspectFit;

和标签:

UILabel *lblName = [[UILabel alloc]init];
lblName.backgroundColor=[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.6];
lblName.frame = CGRectMake(pictureV.bounds.origin.x, pictureV.bounds.origin.y + pictureV.bounds.size.height - 30, self.picture.image.size.width, 30);
[lblName setText:self.pictureNote.text];
[lblName setTextAlignment:UITextAlignmentCenter];
[lblName setTextColor:[UIColor whiteColor]];
[pictureV addSubview:lblName]

将 contentMode 设置为 UIViewContentModeScaleAspectFit 时,有些图像小于我的 UIImageView,我似乎无法使标签的宽度大小相同。

最佳答案

如果您使用 UIViewContentModeScaleAspectFit,图像将不会占据 UIImage View 的完整尺寸。 而不是将 pictureV 的 contentMode 属性设置为 UIViewContentModeScaleToFill , 然后图像将占据您的 imageView 的整个大小

关于ios - 具有 contentMode UIViewContentModeScaleAspectFit 的 UIImageView 的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12424905/

相关文章:

ios - 如何在 Swift 中使用 CG 类创建下面附加的 UI?

ios - 了解何时通过手势关闭分屏 View Controller 的主控

ios - 为什么 UIImageView 在其内容模式为 .scaleAspectFill 时在纵向图片中超出其限制?

iOS UIScrollView 大小 subview 仅一次

ios - UIImageView 阴影和纵横比填充在一起

ios - 何时/如何设置 UIImageView contentMode(UIImageView setContentMode 不起作用)

ios - UIImageView 纵横比?

ios - 在/usr/lib/system/libcache.dylib 中,缺少所需的架构 armv6

iphone - ExclusiveTouch的属性会影响 subview 吗?

ios - Swift 对象初始化(类工厂方法、默认初始化、便捷初始化)