【发布时间】:2022-01-04 12:33:38
【问题描述】:
首先,我使用的是 Xamarin iOS。
每当我尝试设置 UIButton 的图像时,图像都会变得与整个屏幕一样大。我希望该图像适合 UIButton 的边界/框架。
我尝试过使用 PDF 图像和 PNG 图像(屏幕截图中的图像是 png)。它们都忽略了它们嵌入的实际 UIButton 的框架和大小。
这是 UIButton 在 xcode 故事板中的外观。它与超级视图的垂直和水平中间对齐,宽度为超级视图的 0.25 倍,纵横比为 1:1。我也尝试给它一个固定的高度和宽度,但这没有帮助。
我调试了帧大小,但发现它保持不变并且不受 UIButtons 图像的影响。
总结一下到目前为止我尝试过但不起作用的所有内容:
public partial class ViewController : UIViewController
{
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// SetImage -> makes image as big as the screen
Btn.SetImage(UIImage.FromBundle("skip"), UIControlState.Normal);
// SetBackgroundImage -> Image doesn't appear at all, maybe I'm forgetting something?
Btn.SetBackgroundImage(UIImage.FromBundle("skip"), UIControlState.Normal);
// none of these things do literally anything
Btn.ContentMode = UIViewContentMode.ScaleAspectFill;
Btn.ContentMode = UIViewContentMode.ScaleAspectFit;
Btn.ImageView.ContentMode = UIViewContentMode.ScaleAspectFill;
Btn.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
// also have no impact on the image at all
Btn.ImageEdgeInsets = new UIEdgeInsets(100, 100, 100, 100);
Btn.ContentEdgeInsets = new UIEdgeInsets(100, 100, 100, 100);
// also does nothing
UIImage image = UIImage.FromBundle("skip");
image.CreateResizableImage(new UIEdgeInsets(10, 10, 10, 10));
Btn.SetImage(image, UIControlState.Normal);
// no luck again
image.Scale(new CGSize(Btn.Frame.Width, Btn.Frame.Height), 0.1f);
Btn.SetImage(image, UIControlState.Normal);
}
}
我在模拟器上测试的所有设备(iPhone 11、iPhone 12、iPhone 12 mini、iPod touch)上都存在这个问题。我还不能在真实设备上测试它。
似乎互联网上没有其他人有这个问题。我错过了什么?这可能是微不足道的,但我想不通。
提前致谢
【问题讨论】:
-
1) 总是复制代码,不要截图上传
-
2) 按钮的父级是什么?它始终取决于“父布局类”如何处理内部元素。如果它对每个元素说它应该广泛传播,他们就会这样做。仔细看
-
嘿@Tatranskymedved,好吧我更新了问题,所以它直接提供了代码。 UIButton的父级是UIViewController的自动生成的UIView ->
ViewController.View -
您能在这里提供一个基本的、可重现的项目进行测试吗?您可以简单地上传到 github 并在此处附加 repo 链接。
-
@ColeX-MSFT github.com/benbekir/button-image-bug
标签: c# ios xcode xamarin.ios uibutton