FluentLayout for Xamarin.iOS

repository·main·Indexed 40 days ago

https://github.com/fluentlayout/cirrious.fluentlayout

A library providing a fluent API for creating constraint-based layouts in Xamarin.iOS. It simplifies the definition of Auto Layout constraints through extension methods and includes helpers like VerticalStackPanelConstraints for arranging subviews vertically with predictable constraint identifiers.

Tokens
571
Snippets
2
Records
2
Agent score
0%

What's inside FluentLayout

  1. Create constraint-based layouts with FluentLayout

    main

    FluentLayout provides a fluent API for defining Auto Layout constraints in Xamarin.iOS. Instead of manually creating NSLayoutConstraint objects, you can use extension methods on views to define relationships like being atop, below, or aligned with other views using margins.

    View.AddConstraints(
      fNameLabel.AtTopOf(View, vMargin),
      fNameLabel.AtLeftOf(View, hMargin),
      fNameLabel.ToLeftOf(sNameLabel, hMargin),
    
      sNameLabel.WithSameTop(fNameLabel),
      sNameLabel.AtRightOf(View, hMargin),
      sNameLabel.WithSameWidth(fNameLabel),
    
      fNameField.WithSameWidth(fNameLabel),
      fNameField.WithSameLeft(fNameLabel),
      fNameField.Below(fNameLabel, vMargin),
    
      sNameField.WithSameLeft(sNameLabel),
      sNameField.WithSameWidth(sNameLabel),
      sNameField.WithSameTop(fNameField));
  2. Use VerticalStackPanelConstraints for vertical stacks

    main

    For laying out multiple subviews in a vertical stack, use the VerticalStackPanelConstraints helper. This method takes a Margins object and an array of subviews to arrange them vertically.

    When using this helper, constraints are assigned predictable identifiers following the pattern: {containerId}-{viewId}-{constraintDescription}.

    • containerId: The container's AccessibilityIdentifier (or VerticalStackPanel if not set).
    • viewId: The subview's AccessibilityIdentifier (or the subview's index if not set).
    • constraintDescription: A description of the constraint (e.g., Bottom, Top, Width).
    View.AddConstraints(
      View.VerticalStackPanelConstraints(
        new Margins(20, 10, 20, 10, 5, 5),                                              
        View.Subviews));