private void ApplyDesign(Design newDesign) { /* * Change the view controllers and views to display the new design. * Be careful to only change properties that need to be changed. */ // Set the stack view's layout axis to horizontal or vertical. if (this.displayedDesign?.Axis != newDesign.Axis) { this.stackView.Axis = newDesign.Axis; } // Change the view controllers to the small or large kind. if (this.displayedDesign?.Kind != newDesign.Kind) { // Repeat these steps for each of the element view controllers: for (int i = 0; i < this.elementViewControllers.Length; i++) { var oldElementViewController = this.elementViewControllers[i]; // If an old view controller exists, then remove it from this container's child view controllers. if (oldElementViewController != null) { this.RemoveOldElementViewController(oldElementViewController); } // Create the new view controller. var storyboard = UIStoryboard.FromName("Main", null); var newElementViewController = storyboard.InstantiateViewController(newDesign.ElementIdentifier); // Add it as a child view controller of this container. this.AddNewElementViewController(newElementViewController); // And remember it, so we can remove it later. this.elementViewControllers[i] = newElementViewController; } } }
public override void ViewWillLayoutSubviews() { /* * In viewWillLayoutSubviews, we are guaranteed that our view's size, traits, etc. are up to date. * It's a good place to update anything that affects the layout of our subviews. * However, be careful, because this method is called frequently! * Do as little work as possible, and don't invalidate the layout of any superviews. */ // Step 1: Find our size. var size = base.View.Bounds.Size; // Step 2: Decide what design to use, based on our rules. var newDesign = this.DecideDesign(size); // Step 3: If the design is different than what is displayed, change the UI. if (displayedDesign != newDesign) { this.ApplyDesign(newDesign); this.displayedDesign = newDesign; } }