0

如何根据图像和文本的存在来对齐图像和文本。例如,我有 2 个图像,我需要将它们垂直放置在另一个之上,但是当缺少任何一个(未提供)时,另一个应该垂直居中。

4

1 回答 1

1

此代码假定您有基本的 imageview 框架设置(大小和 x 位置)。它将视图集合以相等的间距垂直放置在视图中。如果集合只有一个图像,它甚至会居中。

    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 回答