UIButton带有图像和文字

时间:2013-04-29 23:02:20

标签: mono xamarin.ios uibutton

我知道关于按钮中图像和文本的位置有几个问题,但我仍然无法完成它的工作。我希望标题显示在图像下方。如果没有图像,则标题显示我期望的位置。但是,如果有图像,则不显示标题。

代码粘贴在下面。明天我可以尝试子类化UIButton并调整子视图,但这个应该工作。我认为。你能明白为什么不会吗?

提前致谢!

private static float h = 200.0f;
private static float w = 200.0f;
private static float padding = 10.0f;
private static float textheight = 20f;

for (int i=0; i<numPlants; i++)
{
    var button = UIButton.FromType (UIButtonType.RoundedRect);
    button.Frame = new RectangleF (padding * (i + 1) + (i * w),
                                   padding, w, (h + textheight + 2));
    UIImage img = _group.Plants[i].GetImage();
    if (img != null)
    {
        button.SetImage(img, UIControlState.Normal);
        button.ImageEdgeInsets = new UIEdgeInsets(0.0f, 0.0f, (textheight + 20), 0.0f);
    }

    button.TitleEdgeInsets = new UIEdgeInsets((h+2), 0.0f, 0.0f, 0.0f); 
    button.SetTitleColor(UIColor.Black, UIControlState.Normal);             
    button.SetTitle(_group.Plants[i].Pltnme, UIControlState.Normal);

    plantScrollView.AddSubview (button);

    Plant plt = _group.Plants[i];   
    button.TouchUpInside += (sender, e) => {
        _navController.PushViewController(new SecondViewController(plt), true);
    };
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用XIB或Storyboard,然后添加UIImageView和Label来创建您想要的内容?我觉得这比编码简单得多。

相关问题