0

存在に応じて画像とテキストを整列させる方法。たとえば、2つの画像があり、それらを上下に垂直に配置する必要がありますが、いずれかが欠落している(提供されていない)場合、もう一方は垂直方向の中央に配置する必要があります。

4

1 に答える 1

1

このコードは、基本的なimageviewフレームのセットアップ(サイズとx位置)があることを前提としています。ビューのコレクションを等間隔でビューに垂直に配置します。コレクションに画像が1つしかない場合でも、中央に配置されます。

    UIImageView img1, img2;
        List<UIView> subViews;

        if (img1 != null)
        {
            subViews.Add(img1);
        }


        if (img2 != null)
        {
            subViews.Add(img2);
        }

        float totalHeight = 0;

        foreach (UIView subView in views)
        {
            totalHeight += subView.Frame.Height;                
        }

        float spacing = (this.Bounds.Height - totalHeight) / (subView.Length +1) ;
        float currentY = 0;

        foreach (UIView subView in subViews)
        {
            currentY += spacing;

            if (subView.Superview == null)
            {
                this.Add (subView);
            }

            subView.Frame = new RectangleF(subView.Frame.X,currentY,subView.Frame.Width, subView.Frame.Height);

            currentY += subView.Frame.Height;

        }
于 2012-11-29T13:29:55.793 に答える