如何在 iOS 中的 Xamarin 按钮上将图像左对齐并将文本对齐到中心?
问题描述:
我想将文本居中对齐,但图像在左侧.我已经尝试过使用 HorizontalAlignment,但它同时对齐了两者.怎么做?
I want to align my text in the center, but the image on the left. I have tried it with HorizontalAlignment, but it aligns both. How to do it?
答
你知道我如何从 ButtonRenderer 中的 Control 设置它吗?
do you know how I could set it from Control in ButtonRenderer?
您可以在渲染器中更改按钮的ImageEdgeInsets
和TitleEdgeInsets
以更改图像和标题位置:
You can change the ImageEdgeInsets
and TitleEdgeInsets
of the button in the renderer to change the image and title position:
[assembly: ExportRenderer (typeof(Button), typeof(MyButtonRenderer))]
namespace App650.iOS
{
public class MyButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
//Control is UIButton
Control.ImageEdgeInsets = new UIEdgeInsets(0, Control.Frame.Size.Width - (Control.ImageView.Frame.Size.Width), 0, 0);
Control.TitleEdgeInsets = new UIEdgeInsets(0, 0, 0, Control.ImageView.Frame.Size.Width);
}
}
}
}