无法在导航的第二个屏幕中访问导航栏

Cannot access the Navigation Bar in the second screen of the Navigation

我正在使用 Xamarin.iOS 开发应用程序。它会有很多我无法使用 Main.StoryBoard 设计的屏幕,因此我必须单独设计每个 ViewController。现在我有 2 个屏幕:ViewController.csInProgress.cs。我已将 ViewController.cs 包装在 Main.StoryBoard 中的 NavigationController 中。下面是代码:

ViewController.cs (ViewDidLoad)

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

            //change the navigation bar controller
            this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (26f / 255f, 56f / 255f, 100f / 255f);
            this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes()
            {
                ForegroundColor = UIColor.White
            };
            NavigationController.NavigationBar.BarStyle = UIBarStyle.Black;

            //change the layout of collectionView Layout

            //initialize the data
            sectionCategories = new List<SectionCategory>();
            sectionCategories.Add(new SectionCategory("Lajme", "http://www.ikub.al/media/iOS/lajme.jpg"));
            ......
            sectionCategories.Add(new SectionCategory("Shtypi i dites", "http://www.ikub.al/media/HP/shtypi.jpg"));

            sectionGrid.RegisterClassForCell(typeof(SectionViewCell), SectionViewCell.CellID);
            sectionGrid.Source = new SectionViewSource (sectionCategories,this);
        }

当我单击 sectionGrid 的一个项目时,它会在堆栈中推送一个新的 ViewController

var soonVC = new InProgress ("InProgress");
            soonVC.Title = "InProgress";
            owner.NavigationController.PushViewController(soonVC, true);

InProgress 屏幕有一个 .cs 文件、一个 .desigener.cs 文件和一个 .xib 文件,如下所示:

public partial class InProgress : UIViewController
    {

        String section;
        public InProgress (String text) : base ("InProgress", null)
        {
            section = text;
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            // Perform any additional setup after loading the view, typically from a nib.
        }
        public override void DidReceiveMemoryWarning ()
        {
            base.DidReceiveMemoryWarning ();
            // Release any cached data, images, etc that aren't in use.
        }
    }

[Register ("InProgress")]
    partial class InProgress
    {
        [Outlet]
        [GeneratedCode ("iOS Designer", "1.0")]
        UILabel labelComingSoon { get; set; }

        void ReleaseDesignerOutlets ()
        {
            if (labelComingSoon != null) {
                labelComingSoon.Dispose ();
                labelComingSoon = null;
            }
        }
    }

但是我有两个问题:

我试过一些代码,比如

soonVC.NavigationItem.LeftBarButtonItem.Title = " dsf";

但它给了我一个空对象。

对这两个问题的任何建议。

InProgress.xib

在您的 InProgress class 中,您可以像这样设置后退按钮文本:

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.
        UIBarButtonItem newBackButton = new UIBarButtonItem("dfs",UIBarButtonItemStyle.Plain, (sender,args) => NavigationController.PopViewController (true));
        NavigationItem.SetLeftBarButtonItem (newBackButton, false);
        // The line below will move the search bar below the nav bar
        NavigationController.NavigationBar.Translucent = false;
    }

至于下一个问题:

When I have designed the layout in Xib file I have the label in the center, but in reality its not in the center of the screen because it does not take into consideration the height of the navigation bar, so some layout may be hidden under the Navigation bar.

您在使用自动版式吗?您需要做的就是为垂直居中添加一个约束,它应该在视图中居中。你能上传你的xib吗?如果您不使用自动版式,则只需针对导航控制器进行调整。

更新 如果您将导航栏设置为不透明,它将位于导航栏下方。您可以在 InProgress class 中或在情节提要中进行设置:

我还在你的 xib 中添加了自动布局约束,使你的标签居中,这里是 xib xml:

HTH.