resizableImageWithCapInsets:问题

resizableImageWithCapInsets: issues

我试图弄清楚如何使用 resizableImageWithCapInsets:UIImageView 中使下拉图像适合黑色边框(边框只是为了方便)。

我使用 AutoLayout,_imageView 有固定的高度限制

        UIImage *dropdown = [[UIImage imageNamed:@"dropdown.png"]
                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 100)];
        _imageView.contentMode = UIViewContentModeScaleAspectFit; // modes see below
        _imageView.clipsToBounds = NO;
        _imageView.layer.borderWidth = 1;
        _imageView.image = dropdown;

UIViewContentModeTopLeft

UIViewContentModeScaleToFill

UIViewContentModeScaleAspectFit

UIViewContentModeScaleAspectFill

编辑

我尝试按照@Rahul 的建议设置顶部和底部插图,如果设置 UIViewContentModeScaleToFill 我现在得到的结果(其他模式下的结果相似)。您可能会注意到,白色向下箭头现在略微向下移动

UIImage *dropdown = [[UIImage imageNamed:@"dropdown.png"]
                         resizableImageWithCapInsets:UIEdgeInsetsMake(8, 20, 8, 100)];

编辑 2

在这里更改偏移量似乎没有帮助,问题似乎出在垂直缩放中,好像图像视图出于某种原因只接受一个垂直偏移量,而不是两个,所以我们得到非对称垂直偏移量

编辑 3

GitHub 上的示例项目:ScaleTest-iOS

编辑 4

更新 GitHub 使用自定义大小调整方法的示例(尽管无用),在 Apple Dev Forumtopic 上创建

编辑 5

接受的答案我认为是一种解决方法,我真的更愿意保持固定的高度,但这似乎是不可能的。无论如何,感谢@Rahul 的帮助。

参考本教程:How To Make A Stretchable Button With UIEdgeInsetsMake UIEdgeInsetsMake(8, 8, 8, 8) 并为您的图像应用合适的边缘插图....

UIImage *image = [[UIImage imageNamed:@"dropdown"] resizableImageWithCapInsets:UIEdgeInsetsMake(8, 20, 8, 100)];
_imageView4.layer.borderWidth = 1.0;
_imageView4.clipsToBounds = NO;
_imageView4.contentMode = UIViewContentModeScaleToFill;
_imageView4.image = image;

imageview4 高度设置为 35 像素而不是 40 像素然后完美运行...

_imageView.contentMode = UIViewContentModeScaleAspectFit; 
_imageView.layer.cornerRadius = 20; // Apply your required value.
_imageView.layer.masksToBounds = YES;

试试这个 UIEdgeInsetsMake(8, 100, 8, 8);如果您的图像边界曲线的半径为 20px

,则使用 20px