Search code examples
xamarinlayoutxamarin.iosconstraints

the viewcontroller does not respect the constraints (xamarin.ios)


I was trying a slide transaction on xamarin.ios where the vc2 (the one initialized in the code) flows over the vc1 (the previous one) and is full-screen, however in my code the vc1 interpenetrates with the constraints that are not respected when the animation ends, resulting in this way ..

enter image description here

while the result should be this ..

enter image description here

now I have been trying to solve it case for a while, I ran out of solutions and I still don't understand why it looks like that, the code is this ..

public void SlideTo(UIView View, string A)
    {
        var CC = CGAffineTransform.MakeTranslation(View.Bounds.Size.Width, 0);
        var DD = CGAffineTransform.MakeTranslation(0, 0);

        //Istanzio il vc
        var storyboard = UIStoryboard.FromName("Main", null);
        var vc = storyboard.InstantiateViewController(A);
        
        CALayer gradient = new CALayer();
        gradient.Frame = vc.View.Bounds;
        vc.View.Layer.InsertSublayer(gradient, 1);
        View.AddSubview(vc.View);

        //Centro quella giusta
        vc.View.Transform = true ? CC : DD;

        UIView.Animate(1, 0, UIViewAnimationOptions.CurveLinear, () =>
        {
            vc.View.Transform = true ? DD : CC;


        }, null);
    }

please only solutions for xamarin.ios (not swift)


Solution

  • The safe area is a new way of thinking about the visible space of your application and how constraints are added between a view and a super view.

    Maybe this problem is result from SafeArea.

    Here you can modify code as follow to have a try :

    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);
        
        //suberView is your table view 
        View.Add(suberView);
        suberView.TranslatesAutoresizingMaskIntoConstraints = false;
    
        var safeGuide = View.SafeAreaLayoutGuide;
        suberView.LeadingAnchor.ConstraintEqualTo(safeGuide.LeadingAnchor).Active = true;
        suberView.TrailingAnchor.ConstraintEqualTo(safeGuide.TrailingAnchor).Active = true;
        suberView.TopAnchor.ConstraintEqualTo(safeGuide.TopAnchor).Active = true;
        suberView.BottomAnchor.ConstraintEqualTo(safeGuide.BottomAnchor).Active = true;
    
        View.LayoutIfNeeded();
    }