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 に答える