如何在UITabbar ios中添加分隔符

3

我想在UITabBar上的UITabBarItem之间添加白色分割线,该怎么做?

我在以下链接中找到了相应的objective-C代码:Add separator between section in TabBar

我想要相同效果的c#代码。

1个回答

8
只需在已初始化UITabBarviewDidLoad方法中调用此方法。
 private void setuptabseparator()
            {
                float itemWidth = (float)Math.Floor(this.TabBar.Frame.Size.Width / this.TabBar.Items.Length);
                UIView bgView = new UIView(new CGRect(5, 0, this.TabBar.Frame.Size.Width, this.TabBar.Frame.Size.Height - 5));
                for (int i = 0; i < this.TabBar.Items.Length - 1; i++)
                {
                    float SEPARATOR_WIDTH = 0.8f;
                    UIView separator = new UIView(new CGRect((itemWidth * (i + 1) - SEPARATOR_WIDTH), 0, SEPARATOR_WIDTH, this.TabBar.Frame.Size.Height));
                    separator.BackgroundColor = UIColor.White;
                    bgView.AddSubview(separator);
                }
                UIGraphics.BeginImageContext(bgView.Bounds.Size);
                CGContext context = UIGraphics.GetCurrentContext();
                bgView.Layer.RenderInContext(context);
                UIImage tabbarbackground = UIGraphics.GetImageFromCurrentImageContext();
                this.TabBar.BackgroundImage = tabbarbackground;

           }

我发现我必须在 ViewWillAppear 中调用此方法,否则 Items 集合将为 null - Richard Ockerby

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接