ios - MonoTouch中的自定义UINavigationBar

原文 标签 ios uinavigationcontroller xamarin.ios uinavigationbar

我正在尝试使用monotouch为ios中的导航控制器创建自定义导航栏。我正在开发的应用程序需要在屏幕上显示一个字符串,不管您在应用程序的哪个位置,我不想实现这样的目标(我知道这有点不成比例,但您应该能够理解这一点):

+-----------------------------------------+
|  /---------|                     +---+  |
| /   Back   |     Controller Name |btn|  |
|  \---------|                     +---+  |
+-----------------------------------------+
|     Current building <- global string   |
+-----------------------------------------+

底部的导航条应该尽可能小(同时不难阅读),并且原始导航条可能需要比正常导航条小一点。
另外,我也试过不使用设计器,而是自己编写所有的ui代码,但是如果这不可能实现,那么我将不得不使用设计器。目前,我发现了一个在monotouch中创建自定义uinavigationbar的项目,但是我不知道如何将其应用于我的导航控制器(请参阅,因为navigationbar属性是只读的)。我正在谈论的项目可以在这里找到:https://github.com/mafis/Monotouch-Custom-Control/tree/master/CustomControls。另外,我希望实际导航栏(顶部)的设计是标准的ios设计,并且像导航栏一样正常工作。
任何提示,或如何做到这一点的指针将不胜感激。

最佳答案

我就是这样解决这个问题的。我子类化了uiviewcontroller,如下所示:

using System;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Drawing;
using System.Collections.Generic;
using FdvWeb.Core;
namespace FdvWeb
{
    [MonoTouch.Foundation.Preserve(AllMembers=true)]
    public class MainNavigationController : UINavigationController
    {
        private const float NAV_BAR_HEIGHT = 44;
        private readonly float buildingBarTop = 44;
        private readonly float buildingBarHeight = 17;
        private readonly float viewOffset;

        private readonly HashSet<UIViewController> modifiedViewControllers = new HashSet<UIViewController>();

        UITextView buildingTextView;

        [MonoTouch.Foundation.Preserve]
        public MainNavigationController ()
        {
            viewOffset = buildingBarTop + buildingBarHeight - NAV_BAR_HEIGHT;

            var tt = new UITextView (new RectangleF (0, buildingBarTop, 320, buildingBarHeight));

            NavigationBarHidden = false;
            NavigationBar.AddSubview (tt);
            tt.Font = UIFont.BoldSystemFontOfSize (12);
            tt.TextAlignment = UITextAlignment.Center;
            tt.TextColor = UIColor.LightTextColor;
            tt.BackgroundColor = UIColor.ViewFlipsideBackgroundColor;
            tt.Editable = false;
            tt.ContentInset = new UIEdgeInsets (-9, 0, 0, 0);
            buildingTextView = tt;
        }

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


        }

        public override void ViewWillAppear (bool animated)
        {
            base.ViewWillAppear (animated);
        }

        public string BuildingText
        {
            get {
                return buildingTextView.Text;
            }
            set {
                container.Resolve<IUIThreadDispatcher> ().DispatchOnUIThread (delegate { // Run on UI thread
                    buildingTextView.Text = value;
                });
            }
        }

        public override void PushViewController (UIViewController viewController, bool animated)
        {
            if (!modifiedViewControllers.Contains (viewController))
            {
                viewController.View = new PaddedView (viewController.View, new InnsetF (0, viewOffset, 0, 0));
                modifiedViewControllers.Add (viewController);
            }

            viewController.NavigationItem.RightBarButtonItem = pickBuildingItem;
            base.PushViewController (viewController, animated);
        }

        private class PaddedView : UIView
        {
            private UIView view;
            private InnsetF innsets;

            public PaddedView (UIView view, InnsetF innsets)
                : base(view.Frame)
            {
                this.view = view;
                this.innsets = innsets;
                this.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
                this.AddSubview (view);
            }

            public override void LayoutSubviews ()
            {
                //apply the insets to the subview
                view.Frame = new RectangleF (innsets.Left, innsets.Top,
                    Frame.Size.Width - innsets.Left - innsets.Right,
                    Frame.Size.Height - innsets.Top - innsets.Bottom);
            }
        }

        private class InnsetF
        {
            private float top, bottom, left, right;
            public InnsetF (float left, float top, float right, float bottom)
            {
                this.top = top;
                this.left = left;
                this.bottom = bottom;
                this.right = right;
            }

            public float Top
            {
                get { return top; }
                set { top = value; }
            }

            public float Bottom
            {
                get { return bottom; }
                set { bottom = value; }
            }

            public float Right
            {
                get { return right; }
                set { right = value; }
            }

            public float Left
            {
                get { return left; }
                set { left = value; }
            }
        }
    }
}

相关文章:

ios - 将行距添加到textView不起作用

ios - [iPhone]在Google地图上的某个位置绘制圆圈

ios - Swift 2:iOS 9中的UITableView'reloadSections'方法崩溃

objective-c - 如何将viewcontrollers添加到我的导航控制器?

ios - Swift 3将值传递给UICollectionViewCell按钮上的Controller单击

xamarin - 为什么我不能使用iOS Designer创建序列? -Xamarin Studio

xcode - 如何在Xamarin Forms中集成Apple Pay?

ios - 如何从目标时间中减去当前时间?

ios - ViewController在隐藏的导航栏上具有不同的行为

ios - Monotouch SIGSEV仅在模拟器中