Search code examples
iosxamarinconstraintssyncfusionsfcalendar

xamarin Ios add UIView to ContainerView


I need help align a Subview in ContainerView. I tried to add contraints but it messed up everything so I tried FluentLayout (https://github.com/FluentLayout/Cirrious.FluentLayout)

My Code looks like this:

public override void ViewDidLoad()
        {

            SFCalendar calendar = new SFCalendar();
            containerView.AddSubview(calendar);

            containerView.AtBottomOf(this.View);
            containerView.AtTopOf(this.View);
            containerView.WithSameWidth(this.View);

            calendar.AtBottomOf(containerView);
            calendar.AtTopOf(containerView);
            calendar.WithSameWidth(containerView);
        }

But in the end my Calendar is cut off a bit, what i am doing wrong here?

enter image description here enter image description here


Solution

  • Before you use the Autolayout, you should disable the TranslatesAutoresizingMaskIntoConstraints. So set this two controls property to False will make a trick, moreover you should correct your usage:

    containerView.TranslatesAutoresizingMaskIntoConstraints = false;
    
    SFCalendar calendar = new SFCalendar();
    containerView.AddSubview(calendar);
    
    calendar.TranslatesAutoresizingMaskIntoConstraints = false;
    
    View.AddConstraints(containerView.AtBottomOf(this.View),
    containerView.AtTopOf(this.View),
    containerView.WithSameWidth(this.View));
    
    
    containerView.AddConstraints(calendar.AtBottomOf(containerView),
    calendar.AtTopOf(containerView),
    calendar.WithSameWidth(containerView));
    

    But I recommend you to use the native Constraints to do this, also it seems you want to make this containerView full screen. Add its leading, trailing, top, bottom will be better:

    SFCalendar calendar = new SFCalendar();
    containerView.AddSubview(calendar);
    
    containerView.TranslatesAutoresizingMaskIntoConstraints = false;
    containerView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor).Active = true;
    containerView.TopAnchor.ConstraintEqualTo(TopLayoutGuide.GetBottomAnchor()).Active = true;
    containerView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor).Active = true;
    containerView.BottomAnchor.ConstraintEqualTo(BottomLayoutGuide.GetTopAnchor()).Active = true;
    
    calendar.TranslatesAutoresizingMaskIntoConstraints = false;
    calendar.LeadingAnchor.ConstraintEqualTo(containerView.LeadingAnchor).Active = true;
    calendar.TopAnchor.ConstraintEqualTo(containerView.TopAnchor).Active = true;
    calendar.TrailingAnchor.ConstraintEqualTo(containerView.TrailingAnchor).Active = true;
    calendar.BottomAnchor.ConstraintEqualTo(containerView.BottomAnchor).Active = true;