0

我是 Xamarin IOS 的新手,我尝试将 stackView 放入 scrollView。然后我生成这段代码。

public class ViewCategory : UIViewController, IIndicatorInfoProvider
{

    public override string Title { get; set; }
    public UIColor Color { get; set; }
    public string Text { get; set; }
    public string Image { get; set; }

    public UIScrollView _scrollView;
    public UIStackView _stackView;
    public ViewCategory(IntPtr handle) : base(handle) { }
    public ViewCategory(string title, UIColor color, string text, string image)
    {
        this.Title = title;
        this.Color = color;
        this.Text = text;
        this.Image = image;
    }

    public IndicatorInfo IndicatorInfoForPagerTabStrip(PagerTabStripViewController pagerTabStripController)
    {
        return new IndicatorInfo(Title);
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        _scrollView = new UIScrollView();

        _stackView = new UIStackView();


        _stackView.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, UIScreen.MainScreen.Bounds.Size.Height);
        _stackView.Axis = UILayoutConstraintAxis.Vertical;
        _stackView.Spacing = 10;





        // Image
        var image = new UIImageView(UIImage.FromBundle(this.Image));
        image.Frame = new Rectangle(0, 0, (int)View.Frame.Width, 150);
        image.ContentMode = UIViewContentMode.ScaleAspectFit;
        _stackView.AddArrangedSubview(image);

        //// define coordinates and size of the circular view
        //// define coordinates and size of the circular view

        float height = 80;
        float width = (float)UIScreen.MainScreen.Bounds.Size.Width * 2 + 40;
        float x = (width / 4 * -1) - 10;
        float y = 100 + ((100 - height) / 2);

        // corner radius needs to be one half of the size of the view
        RectangleF frame = new RectangleF(x, y, width, height);
        // initialize button
        UIButton circularView = new UIButton(frame);
        // set corner radius
        circularView.Layer.CornerRadius = width / 2;
        // set background color, border color and width to see the circular view
        circularView.BackgroundColor = UIColor.Blue;
        circularView.Layer.BorderColor = UIColor.White.CGColor;
        circularView.Layer.BorderWidth = 1;
        // add button to view controller
        _stackView.AddArrangedSubview(circularView);


        var pg_top_background = new UIImageView(UIImage.FromBundle("cat_gauge_top_background.png"));
        pg_top_background.Frame = new CGRect((UIScreen.MainScreen.Bounds.Size.Width - pg_top_background.Image.Size.Width) / 2, 150, pg_top_background.Image.Size.Width, pg_top_background.Image.Size.Height);
        _stackView.AddArrangedSubview(pg_top_background);

        var pg_top_foreground = new UIImageView(UIImage.FromBundle("cat_gauge_top_general.png"));
        pg_top_foreground.Frame = new CGRect((UIScreen.MainScreen.Bounds.Size.Width - pg_top_foreground.Image.Size.Width) / 2, 150, pg_top_foreground.Image.Size.Width, pg_top_foreground.Image.Size.Height);

        _stackView.AddArrangedSubview(pg_top_foreground);

        var pg_bottom_background = new UIImageView(UIImage.FromBundle("cat_gauge_bottom_background.png"));
        pg_bottom_background.Frame = new CGRect((UIScreen.MainScreen.Bounds.Size.Width - pg_bottom_background.Image.Size.Width) / 2, 150 + pg_top_foreground.Image.Size.Height + 5, pg_bottom_background.Image.Size.Width, pg_bottom_background.Image.Size.Height);
        _stackView.AddArrangedSubview(pg_bottom_background);

        var pg_bottom_foreground = new UIImageView(UIImage.FromBundle("cat_gauge_bottom_foreground.png"));
        pg_bottom_foreground.Frame = new CGRect((UIScreen.MainScreen.Bounds.Size.Width - pg_bottom_foreground.Image.Size.Width) / 2, 150 + pg_top_foreground.Image.Size.Height + 5, pg_bottom_foreground.Image.Size.Width, pg_bottom_foreground.Image.Size.Height);
        _stackView.AddArrangedSubview(pg_bottom_foreground);


        var label = new UILabel();
        label.Text = this.Text;
        label.Font = label.Font.WithSize(12);
        label.Frame = new CGRect(0, 180, 100, 20);

        //label.TextAlignment = UITextAlignment.Center;
        label.Center = new CGPoint(UIScreen.MainScreen.Bounds.Size.Width / 2, label.Frame.Y);
        _stackView.AddArrangedSubview(label);


        _scrollView.AddSubview(_stackView);
        _scrollView.Frame = new RectangleF(0, 0, (float)View.Frame.Width, (float)_stackView.Frame.Height);

        _scrollView.ContentSize = new SizeF((float)View.Frame.Width, (float)((float)_stackView.Frame.Height + 50));

        View.AddSubview(_scrollView);

    }
}

我不明白的一件事。为什么我必须将我的元素放置在我的堆栈中,如果它感觉是一个堆栈布局,例如:元素 A 元素 B 元素 C

?

然后我画一个图像,如果图像真的很大,其他元素就无法显示。

有人能帮我吗 ?

谢谢

4

0 回答 0