0

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

代码粘贴在下面。明天我可以尝试继承 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);
    };
}
4

1 回答 1

0

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

于 2013-04-29T23:08:51.840 回答