2

I have a custom view on the storyboard, which acts as a toolbar. This view has three buttons and I define custom views for each button in the ViewDidLoad.

When I navigate to a different view, then move backwards to this original screen, both the text and images on these buttons are missing. There's only an empty black bar. This happens for some devices but the views are displayed just fine and never disappear on other devices.

What I've tried:

  • I've explicitly set the hidden property of these buttons to false in the ViewWillAppear

    btn1.Hidden = false; btn2.Hidden = false; btn3.Hidden = false;

  • I've also tried defining the buttons inside the ViewWillAppear instead of the ViewDidLoad, to see if that solves the problem. They still disappear.

Below is my code:

  public override void ViewWillAppear(bool value)
    {
        try
        {
            base.ViewWillAppear(value);


            //icon setup

            //the space between the image and modelFilterText
            var spacing = 0.3f;

            var icon1 = UIImage.FromBundle("Icons/Home/icon1.png");


            btn1.SetImage(icon1, UIControlState.Normal);
            btn1.SetTitle("ONE", UIControlState.Normal);
            btn1.SetTitleColor(UIColor.LightGray, UIControlState.Normal);
            btn1.Font = UIFont.FromName("BankGothicBT-Light", 12f);
            btnRequest.TouchUpInside += (object sender, EventArgs e) =>
            {
                NavigationItem.BackBarButtonItem = new UIBarButtonItem("", UIBarButtonItemStyle.Plain, null);

                NavigationController.PushViewController(view1, true);
            };

            // lower the modelFilterText and push it left so it appears centered 
            //  below the image

            var imageSize = btn1.ImageView.Image.Size;
            btn1.TitleEdgeInsets = new UIEdgeInsets(0.0f, -imageSize.Width, -(imageSize.Height + spacing),
                0.0f);

            // raise the image and push it right so it appears centered
            //  above the modelFilterText

            var titleSize = btn1.TitleLabel.Text.StringSize(btn1.TitleLabel.Font);
            btn1.ImageEdgeInsets = new UIEdgeInsets(-(titleSize.Height + spacing), 0.0f, 0.0f,
                -titleSize.Width);

            //Second Icon
            var icon2 = UIImage.FromBundle("Icons/Home/icon2.png");

            btn2.SetImage(icon2, UIControlState.Normal);
            btn2.SetTitle("TWO", UIControlState.Normal);
            btn2.SetTitleColor(UIColor.LightGray, UIControlState.Normal);
            btn2.Font = UIFont.FromName("BankGothicBT-Light", 12f);

            btn2.TouchUpInside +=
                (object sender, EventArgs e) =>
            {
                NavigationItem.BackBarButtonItem = new UIBarButtonItem("", UIBarButtonItemStyle.Plain, null);
            };

            // lower the modelFilterText and push it left so it appears centered 
            //  below the image

            var imgSize = btn2.ImageView.Image.Size;
            btn2.TitleEdgeInsets = new UIEdgeInsets(0.0f, -imgSize.Width, -(imgSize.Height + spacing), 0.0f);

            // raise the image and push it right so it appears centered
            //  above the modelFilterText

            var titleLength = btn2.TitleLabel.Text.StringSize(btn2.TitleLabel.Font);
            btn2.ImageEdgeInsets = new UIEdgeInsets(-(titleLength.Height + spacing), 0.0f, 0.0f,
                -titleLength.Width);


            //Third Icon
            var icon3 = UIImage.FromBundle("Icons/Home/icon3.png");

            btn3.SetImage(icon3, UIControlState.Normal);
            btn3.SetTitle("THREE", UIControlState.Normal);
            btn3.SetTitleColor(UIColor.LightGray, UIControlState.Normal);
            btn3.Font = UIFont.FromName("BankGothicBT-Light", 12f);

            btn3.TouchUpInside += (object sender, EventArgs e) =>
            {

                NavigationItem.BackBarButtonItem = new UIBarButtonItem("", UIBarButtonItemStyle.Plain, null);

                NavigationController.PushViewController(view3, true);
            };


            // lower the modelFilterText and push it left so it appears centered below the image

            var imagSize3 = btn3.ImageView.Image.Size;
            btn3.TitleEdgeInsets = new UIEdgeInsets(0.0f, -imagSize3.Width, -(imagSize3.Height + spacing),
                0.0f);

            // raise the image and push it right so it appears centered
            //  above the modelFilterText

            var titleLength3 = btn3.TitleLabel.Text.StringSize(btn3.TitleLabel.Font);
            btn3.ImageEdgeInsets = new UIEdgeInsets(-(titleLength3.Height + spacing), 0.0f, 0.0f,
                -titleLength3.Width);


        }
        catch (Exception ex)
        {
            Console.WriteLine("View will appear error " + ex.Message + ex.StackTrace);
        }

    }

The buttons appear and are properly drawn the first time. However, after I navigate away from this page and come back to it, there's only a black view at the bottom. No buttons.

naffie
  • 679
  • 1
  • 12
  • 29

1 Answers1

1

I've learnt that the buttons don't actually disappear. They are obscured by the iOS toolbar.

For instance, if I navigate to a screen which has an iOS toolbar, then navigate backwards back to this home page, the NavigationController.SetToolbarHidden property is set to true for all the other screens after that.

So all I needed to do was set this to false in the ViewWillAppear of this Home Page, so it doesn't obscure my custom view.

NavigationController.SetToolbarHidden(true,false)
naffie
  • 679
  • 1
  • 12
  • 29