Clancey / FlyoutNavigation

Other
95 stars 87 forks source link

Views using NSLayoutConstraints Fail to Properly Draw on First Use #29

Open benhysell opened 11 years ago

benhysell commented 11 years ago

I have an odd behavior I'm trying to track down, and I'm not sure if it is FlyoutNavigation or something else I'm doing. Maybe someone can take a quick look who understands things a bit better than I do.

Example Project - https://github.com/benhysell/FlyoutNavigationWithNSLayoutConstraintsError

Goal - Use https://gist.github.com/praeclarum/5175100, A C# syntax for NSLayoutConstraints, described in this blog post, http://praeclarum.org/post/45690317491/easy-layout-a-dsl-for-nslayoutconstraint, with FlyoutNavigation.

Issue - On the first use of a view that incorporates NSLayoutConstraints the view doesn't respect the constraints or background color, both odd. On subsequent 'selections' of the view from the menu of FlyoutNavigation the view will properly draw.

Setup - Working in Xamarin Beta Channel against iPhone Simulator 6.1 and the latest released Xcode.

Steps to Reproduce

  1. The easiest way to show this is to open the sample project that comes with FlytoutNavigation and modify this project using the steps below. I included in this post a link to the example project I modified to show the error.
  2. Add the gist, https://gist.github.com/praeclarum/5175100, to a new class, call it layout.
  3. Add a new UIViewController and the following to ViewDidLoad(), note this was modified from the Xamarin 'Hello World' sample app one can create in VS2012

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

       View.Frame = UIScreen.MainScreen.Bounds;
       View.BackgroundColor = UIColor.Red;
       button = UIButton.FromType(UIButtonType.RoundedRect);
    
       button.SetTitle("Click me", UIControlState.Normal);
    
       button.TouchUpInside += (object sender, EventArgs e) =>
       {
           button.SetTitle(String.Format("clicked {0} times", numClicks++), UIControlState.Normal);
       };
    
       View.AddSubview(button);
       const int ButtonWidth = 75;
       const int HPadding = 22;
       const int VPadding = 44;
       View.ConstrainLayout(() =>
                             button.Frame.Width == ButtonWidth &&
           button.Frame.Left == View.Frame.Left + HPadding &&
           button.Frame.Top == View.Frame.Top + VPadding);
    }
  4. In the MainController.cs replace

navigation.ViewControllers = Array.ConvertAll (Tasks, title => new UINavigationController (new TaskPageController (navigation, title)) );

with

navigation.ViewControllers = Array.ConvertAll(Tasks, title => new UINavigationController(new MyViewController(navigation)) );

I'm saying 'make every view a view that implements NSLayoutConstraints'.

  1. Run Application, first view returns with: ios simulator screen shot jul 19 2013 10 59 44 pm
  2. Select the same item from the FlyoutNavigation menu and it will then properly draw. ios simulator screen shot jul 19 2013 11 01 00 pm
  3. I've traced through FlyoutNavigationController.cs a couple of times and it appears on the second time selecting the item from FlyoutNavigation, on line 238:

this.View.AddSubview (mainView);

ViewControllers[0].ChildViewControllers[0].View.Frame {{X=160,Y=208,Width=0,Height=0}} System.Drawing.RectangleF

This is the incorrect size for the view, however after I step over line 238:

ViewControllers[0].ChildViewControllers[0].View.Frame {{X=0,Y=0,Width=320,Height=416}} System.Drawing.RectangleF

The position is fixed, and the view will draw correctly.

Summary I've tried using the gist with the NSLayoutConstraints by itself in a single page window application without issue, and I'm thinking since it does eventually draw properly after a second invokation of FlyoutNavigation I'm thinking there is a 'something' I'm missing with the FlyoutNavigation, or setting incorrectly that I can't put my finger on.

benhysell commented 11 years ago

Also tried on real hardware, iPad version 3 with iOS 6.1...same issue as simulator. Cross-posted on StackOverflow, http://stackoverflow.com/questions/17794440/flyoutnavigation-views-using-nslayoutconstraints-fail-to-properly-draw-view-on.