控制器中的自定义工具栏

控制器中的自定义工具栏

本文介绍了UINavigation 控制器中的自定义工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解在 Xamarin.ios 中自定义我自己的工具栏应该采用什么方法.导航控制器带有自己的默认工具栏,但如何更改高度并拥有自己的按钮、背景图像.

I am having trouble understanding what approach to take to customize my own Toolbar in Xamarin.ios. The Navigation controller comes with its own default toolbar but how can i change the height and have my own buttons, background image.

以上的最佳方法是什么?

What is the best approach for the above ?

推荐答案

您可以根据需要创建自定义导航栏.

You can create a custom navigationBar as you want .

public class xxxViewController: UIViewController
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);



            NavigationController.NavigationBar.Hidden = true;


            double height = IsiphoneX();

            UIView backView = new UIView()
            {
                BackgroundColor = UIColor.White,
                Frame = new CGRect(0,20,UIScreen.MainScreen.Bounds.Width, height),

            };


            UIButton backBtn = new UIButton() {

                Frame = new CGRect(20, height-44, 40, 44),
                Font = UIFont.SystemFontOfSize(18),

            } ;
            backBtn.SetTitle("Back", UIControlState.Normal);
            backBtn.SetTitleColor(UIColor.Blue, UIControlState.Normal);
            backBtn.AddTarget(this,new Selector("GoBack"),UIControlEvent.TouchUpInside);

            UILabel titleLabel = new UILabel() {
                Frame=new CGRect(UIScreen.MainScreen.Bounds.Width/2-75, 0,150, height),
                Font = UIFont.SystemFontOfSize(20),
                Text = "xxx",
                TextColor = UIColor.Black,
                Lines = 0,

            };

            UILabel line = new UILabel() {

                Frame = new CGRect(0, height, UIScreen.MainScreen.Bounds.Width, 0.5),
                BackgroundColor = UIColor.Black,

            };

            backView.AddSubview(backBtn);
            backView.AddSubview(titleLabel);
            backView.AddSubview(line);

            View.AddSubview(backView);
        }


         double IsiphoneX()
        {

            double height = 44;

            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                if (UIApplication.SharedApplication.Delegate.GetWindow().SafeAreaInsets.Bottom > 0.0)
                {
                    height = 64;
                }
            }

            return height;
        }

        [Export("GoBack")]
        void GoBack()
        {
            NavigationController.PopViewController(true);
        }

        public override void ViewWillDisappear(bool animated)
        {
            base.ViewWillDisappear(animated);

            NavigationController.NavigationBar.Hidden = false;
        }

    }

您可以根据需要设置 title 、 backButton 和 navigationBar 的属性(例如 text 、 color 、BackgroundColor 、font 等)

You can set the property of title , backButton and navigationBar as you need (such as text , color ,BackgroundColor ,font e.g.)

这篇关于UINavigation 控制器中的自定义工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:22