Dynamic Constraints

I tend to arrange my views using View.AddConstraints, which works nicely.

However on one particular view controller I have a UISearchBar with a UITableList below it. Using View.AddConstraints, the UITableList fills the View from the bottom of the UISearchBar to the bottom of the View.

Under some circumstances, when the List is populated I would like to display a MKMapView as well, and can do this on a static, permanent basis, by filling the main View from the bottom of the searchable to the bottom of the view with a "parentView", and then using View.AddConstraints to position the UITableList above the MKMapView inside the parent view, taking 50% of the available height, each.

This is all fine - however, what I need to do is to make the map display dynamic. Sometimes I want to display just the UITableList, and other times, depending on the results I want to share the space with a MKMapView.

What is best practise for doing this? Is it to throw away all the constraints for a View and rebuilt them on the fly.... or is there a better way.

Thanks !

Tagged:

Posts

  • adamkempadamkemp USInsider, Developer Group Leader mod

    There is a method called UpdateViewConstraints. That is the appropriate place to make modifications to the constraints. If something changes that would invalidate the constraints then you call View.NeedsUpdateViewConstraints(), which will trigger a call to UpdateViewConstraints.

  • MarthughieMarthughie GBMember ✭✭

    @adamkemp many thanks for your response.

  • NicholasTurnerNicholasTurner USMember, University ✭✭
    edited October 2015

    @Marthughie

    What I do in my view. I have 3 views horizontal and the middle view disappears quite often. I save a variable for the width constraint.

    So lets Say I want to hide that view I do

    `NSLayoutConstrant width _widthNSLC = ...., 200f); 200 width view

    // Animate - closing and animated
    MyView.SetNeedsLayout(); // reset size
    _widthNSLC.Constant = 0f;

    UIView.Animate(.5, // half a second length
    0,
    UIViewAnimate.ShowHideTransition,
    () => {
    MyView.Alpha = 0f; // slowly hide the view
    MyView.LayoutIfNeeded();
    MyView.SetNeedsLayout();
    MyView.InvalidateIntrinsicSize();
    },
    () => {}, //logic for animation completed
    );

    `

    This will hide your map view, Now with the Views I set the following constraints

    View1 Width is a 50% of it's parent
    View 2 Width is 200, It's Left Constraint is Bound to View1's Right
    View 3 Right is equal to Right of Parent, It's Left is bound to view2's right

    You can set hugging priorities also ( 250 ) so they slide in am animated way equally.

    So for your example

    If would write this - this is slightly complicated - watch AutoLayoutByExample - https://developer.apple.com/videos/wwdc/2012/#232

    View 1 Left = Parent.Left
    View 1 Right = View2.Left

    View 2 Left = View1.Right
    View 2 Right = Parent.Right
    View 2 Width.Constant = 50% of parent View ( so it can be animated )

    View1.Width = Parent.Width, Multiplier = .5f, Relationship.GreaterThanOrEqual, Priority = 750 - forces a minimum for 50%

    Note: View2.Right can be 2 things

    This will animate the closing of your map over half a second. I also add a slight blur and with the Alpha going to 0 it will blur as it hides and closes at the same time giving a nice fluid and elegant closing.

  • MarthughieMarthughie GBMember ✭✭

    @NicholasTurner. Great post. Thank you :smile:

Sign In or Register to comment.