首页 > 解决方案 > 在按钮上使用 FFImageLoading 时如何设置内容模式

问题描述

当我使用 FFImageLoading 库设置图像时,我无法让内容模式在按钮上工作。我想将模式设置为 ScaleAspectFill。

我尝试设置 button.imageview 的内容模式以及按钮本身。

button.ContentMode = UIViewContentMode.ScaleAspectFill;
button.ImageView.ContentMode = UIViewContentMode.ScaleAspectFill;
ImageService.Instance.LoadUrl(image_url).Into(button);

图像未缩放并且显示为 ScaleAspectFit 而不是 Fill

标签: iosxamarin.iosffimageloading

解决方案


Setting the button's ImageView's ContentMode will adjust the image's representation.

I used the code below to display two buttons. One has set the ContentMode and another doesn't:

UIButton btn = new UIButton(UIButtonType.Custom);
btn.Frame = new CoreGraphics.CGRect(50, 100, 100, 40);

btn.ImageView.ContentMode = UIViewContentMode.ScaleAspectFill;
ImageService.Instance.LoadUrl("https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Mu3b?ver=5c31").Into(btn);

UIButton btn2 = new UIButton(UIButtonType.Custom);
btn2.Frame = new CoreGraphics.CGRect(50, 200, 100, 40);
ImageService.Instance.LoadUrl("https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Mu3b?ver=5c31").Into(btn2);

View.AddSubview(btn);
View.AddSubview(btn2);

They have different effects:

enter image description here

The first button doesn't have enough width to display this image so it clips the left and right edges due to the configuration of content mode. But the second button stretches the image to fit the whole space. Both buttons have the same size.


推荐阅读