xamarin - 在 Xamarin IOS 中显示带有文本和图像的按钮

标签 xamarin xamarin.ios

我想在 Xamarin IOS 中设计一个自定义按钮来显示文本和图像。 请找到下面的图片。

enter image description here

您可以在上图中找到文本和向下箭头符号。 向下箭头符号应始终显示在文本之后。按钮文本是动态的。

有人可以建议我解决这个问题吗?

最佳答案

您想要使用 UIButtonTitleEdgeInsetsImageEdgeInsets 属性来控制文本和图像的插入开始位置:

button.TitleEdgeInsets = new UIEdgeInsets(0, -button.ImageView.Frame.Size.Width, 0, button.ImageView.Frame.Size.Width);
button.ImageEdgeInsets = new UIEdgeInsets(0, button.TitleLabel.Frame.Size.Width, 0, -button.TitleLabel.Frame.Size.Width);

完整示例:

<小时/>
    button = new UIButton(UIButtonType.System);
    button.Frame = new CGRect(150, 20, 200, 50);
    button.Layer.CornerRadius = 5;
    button.BackgroundColor = UIColor.Black;
    button.SetTitleColor(UIColor.White, UIControlState.Normal);
    button.SetImage(UIImage.FromFile("ratingstar.png"), UIControlState.Normal);
    button.SetTitle("StackOverflow", UIControlState.Normal);
    button.TitleEdgeInsets = new UIEdgeInsets(0, -button.ImageView.Frame.Size.Width, 0, button.ImageView.Frame.Size.Width);
    button.ImageEdgeInsets = new UIEdgeInsets(0, button.TitleLabel.Frame.Size.Width, 0, -button.TitleLabel.Frame.Size.Width);
<小时/>

enter image description here

关于xamarin - 在 Xamarin IOS 中显示带有文本和图像的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37089317/

相关文章:

xaml - Xamarin.Forms:创建 SearchBar

c# - Azure移动服务: Define a Table only locally

android - Xamarin 门户类库访问 ASP.NET Web ApiController

c# - MonoTouch 的性能问题?

c# - 使用 .NET 标准库的 Xamarin.Android 上的 JsonSerializationException 'Unable to find a constructor'

c# - 如何在 Xamarin.Forms 中用引号将字符串括起来

c# - StyledStringElement 'Tapped' 事件

ios - 有没有办法在 MonoTouch 中制作原生的 cocoa 类库?

c# - 从 MonoTouch 中引用的 DLL 获取执行程序集名称?

c# - MonoTouch 中的 ViewForZoomingInScrollView