[英]Make UIButton Image fit into the Button
首先,我使用的是Xamarin iOS。
每當我嘗試設置 UIButton 的圖像時,圖像會變得與整個屏幕一樣大。 我希望該圖像適合 UIButton 的邊界/框架。
我嘗試使用 PDF 圖像和 PNG 圖像(屏幕截圖中的圖像是 png)。 它們都忽略了它們嵌入的實際 UIButton 的框架和大小。
這是 UIButton 在 xcode storyboard 中的樣子。 它與超級視圖的垂直和水平中間對齊,寬度為超級視圖的 0.25 倍,縱橫比為 1:1。 我也嘗試給它一個固定的高度和寬度,但這沒有幫助。
我調試了幀大小,但發現它保持不變並且不受 UIButtons Image 的影響。
總結一下我迄今為止嘗試過的所有事情並且不起作用:
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)上都存在這個問題。 我還不能在真實設備上測試它。
似乎互聯網上沒有其他人有這個問題。 我錯過了什么? 這可能是微不足道的,但我無法弄清楚。
提前致謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.