自定义TabbedRenderer-iOS顶部的标签栏

时间:2018-11-15 18:46:26

标签: xamarin.forms xamarin.ios tabbar tabbedpage

我正在做一个CustomTabbedRenderer来在iOS页面的顶部设置标签栏。 这些TopTabbedBar嵌套为根选项卡式页面的选项卡。

我首先得到了想要的东西(参见图片): right layout 但是,当我选择另一个选项卡时,将调整Page.ContainerArea的大小(参见图片),并且coontrol位于选项卡的后面。 bad layout

public class TabbedPageCustomRenderer : TabbedRenderer
{
        bool _loaded;
        Size _queuedSize;



        Page Page => Element as Page;


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

            if (Element == null)
                return;

            if (!Element.Bounds.IsEmpty)
            {   
        // I add "(loat)10" to shift the View.Frame under the statusbar

                View.Frame = new System.Drawing.RectangleF((float)Element.X, (float)10, (float)Element.Width, (float)(Element.Height-10));
            }

            var tabBarFrame = TabBar.Frame;

            var frame = ParentViewController != null ? ParentViewController.View.Frame : View.Frame;
            var height = frame.Height - tabBarFrame.Height;

        // I add "tabBarFrame.Height" to shift the "Page.ContainerArea" under the Tabbar
            Page.ContainerArea = new Rectangle(0, tabBarFrame.Height, frame.Width, height);


            if (!_queuedSize.IsZero)
            {
                Element.Layout(new Rectangle(Element.X, Element.Y, _queuedSize.Width, _queuedSize.Height));
                _queuedSize = Size.Zero;
            }

            _loaded = true;
        }

        public override void ViewWillLayoutSubviews()
        {
            base.ViewWillLayoutSubviews();
            this.TabBar.InvalidateIntrinsicContentSize();


            nfloat tabSize = 44.0f;

            UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;

            if (UIInterfaceOrientation.LandscapeLeft == orientation || UIInterfaceOrientation.LandscapeRight == orientation)
            {
                tabSize = 32.0f;
            }

            var tabFrame = this.TabBar.Frame;
            tabFrame.Height = tabSize;
            tabFrame.Y = View.Frame.Y;
            this.TabBar.Frame = tabFrame;

            //this.TabBar.TopAnchor = false;
            this.TabBar.Translucent = false;
            this.TabBar.Translucent = true;
        }
    }

当我点击一个新选项卡时,我发现“ SafeAreaInsets”已被重置。可能是问题所在。

能帮我吗?

0 个答案:

没有答案