iOS标签栏背景图片命名

时间:2017-06-22 11:14:33

标签: ios xamarin tabs separator

我想以编程方式在我的tabor中的项之间创建一个分隔符。然后我写了这段代码,但它没有显示任何内容。

        float separator_width = 3f;
        UIColor separator_color = UIColor.Red;

        float itemWidth = (float)Math.Floor(this.TabBar.Frame.Width / this.TabBar.Items.Length);

        UIView bgView = new UIView();
        bgView.Frame = new CGRect(0, 0, this.TabBar.Frame.Width, this.TabBar.Frame.Height);

        for (int i = 0; i < this.TabBar.Items.Length; i++)
        {
            UIView separator = new UIView();
            separator.Frame = new CGRect(itemWidth * (i + 1) - separator_width / 2, 0, separator_width, this.TabBar.Frame.Height);
            bgView.Add(separator);
        }

        UIGraphics.BeginImageContext(new CGSize(bgView.Frame.Width, bgView.Frame.Height));

        bgView.Layer.RenderInContext(UIGraphics.GetCurrentContext());

        UIImage tabBarBackground = UIGraphics.GetImageFromCurrentImageContext();

        UIGraphics.EndImageContext();



        UITabBar.Appearance.BackgroundImage = tabBarBackground;

我试着写它并在另一个视图中显示但仍然没有。我该怎么办?

由于

1 个答案:

答案 0 :(得分:0)

我找到了另一个解决方案。对于那些搜索它的人:

请查看以下内容:

        var image = new UIImage();
        var path = new UIBezierPath();
        var lineHeight = 42.5f;
        float itemWidth = (float)Math.Floor(this.TabBar.Frame.Width / this.TabBar.Items.Length);

        UIGraphics.BeginImageContextWithOptions(new CGSize(this.TabBar.Frame.Width, this.TabBar.Frame.Height), false, 0);

        image.Draw(new CGPoint(0,0));

        UIColor.Clear.FromHex(0xF2F2F2).SetStroke();
        path.LineWidth = 1;


        for (int i = 1; i < this.TabBar.Items.Length; i++)
        {
            path.MoveTo(new CGPoint(itemWidth * i - path.LineWidth / 2, (this.TabBar.Frame.Height - lineHeight) / 2));
            path.AddLineTo(new CGPoint(itemWidth * i - path.LineWidth / 2, lineHeight + (this.TabBar.Frame.Height - lineHeight) / 2));
        }

        path.Stroke();
        image = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();

        UITabBar.Appearance.BackgroundImage = image;