Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for general layout guides #63

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions Cirrious.FluentLayout/AdvancedFluentLayoutExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,87 @@ public static class AdvancedFluentLayoutExtensions

public static FluentLayout AtTopOf(this UIView view, UIView parentView, nfloat? margin = null) =>
view.Top().EqualTo().TopOf(parentView).Plus(margin.GetValueOrDefault(DefaultMargin));

public static FluentLayout AtTopOfLayoutGuide(this UIView view, UILayoutGuide layoutGuide, nfloat? margin = null) =>
view.Top().EqualTo().TopOf(layoutGuide).Plus(margin.GetValueOrDefault(DefaultMargin));

public static FluentLayout AtTopOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(11, 0)
? view.Top().EqualTo().TopOf(parentView.SafeAreaLayoutGuide).Plus(margin.GetValueOrDefault(DefaultMargin))
: view.AtTopOf(parentView, margin);
? view.AtTopOfLayoutGuide(parentView.SafeAreaLayoutGuide, margin)
: view.AtTopOf(parentView, margin);

public static FluentLayout AtTopOfMargins(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(9, 0)
? view.AtTopOfLayoutGuide(parentView.LayoutMarginsGuide, margin)
: view.AtTopOf(parentView, margin);

public static FluentLayout AtTopOfReadableContent(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(9, 0)
? view.AtTopOfLayoutGuide(parentView.ReadableContentGuide, margin)
: view.AtTopOf(parentView, margin);

public static FluentLayout AtLeftOf(this UIView view, UIView parentView, nfloat? margin = null) =>
view.Left().EqualTo().LeftOf(parentView).Plus(margin.GetValueOrDefault(DefaultMargin));

public static FluentLayout AtLeftOfLayoutGuide(this UIView view, UILayoutGuide layoutGuide, nfloat? margin = null) =>
view.Left().EqualTo().LeftOf(layoutGuide).Plus(margin.GetValueOrDefault(DefaultMargin));

public static FluentLayout AtLeftOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(11, 0)
? view.Left().EqualTo().LeftOf(parentView.SafeAreaLayoutGuide).Plus(margin.GetValueOrDefault(DefaultMargin))
? view.AtLeftOfLayoutGuide(parentView.SafeAreaLayoutGuide, margin)
: view.AtLeftOf(parentView, margin);

public static FluentLayout AtLeftOfMargins(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(11, 0)
? AtLeftOfLayoutGuide(view, parentView.LayoutMarginsGuide, margin)
: view.AtLeftOf(parentView, margin);

public static FluentLayout AtLeftOfReadableContent(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(9, 0)
? view.AtLeftOfLayoutGuide(parentView.ReadableContentGuide, margin)
: view.AtLeftOf(parentView, margin);

public static FluentLayout AtRightOf(this UIView view, UIView parentView, nfloat? margin = null) =>
view.Right().EqualTo().RightOf(parentView).Minus(margin.GetValueOrDefault(DefaultMargin));

public static FluentLayout AtRightOfLayoutGuide(this UIView view, UILayoutGuide layoutGuide, nfloat? margin = null) =>
view.Right().EqualTo().RightOf(layoutGuide).Minus(margin.GetValueOrDefault(DefaultMargin));

public static FluentLayout AtRightOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(11, 0)
? view.Right().EqualTo().RightOf(parentView.SafeAreaLayoutGuide).Minus(margin.GetValueOrDefault(DefaultMargin))
? view.AtRightOfLayoutGuide(parentView.SafeAreaLayoutGuide, margin)
: view.AtRightOf(parentView, margin);

public static FluentLayout AtRightOfMargins(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(11, 0)
? view.AtRightOfLayoutGuide(parentView.LayoutMarginsGuide, margin)
: view.AtRightOf(parentView, margin);

public static FluentLayout AtRightOfReadableContent(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(9, 0)
? view.AtRightOfLayoutGuide(parentView.ReadableContentGuide, margin)
: view.AtRightOf(parentView, margin);

public static FluentLayout AtBottomOf(this UIView view, UIView parentView, nfloat? margin = null) =>
view.Bottom().EqualTo().BottomOf(parentView).Minus(margin.GetValueOrDefault(DefaultMargin));

public static FluentLayout AtBottomOfLayoutGuide(this UIView view, UILayoutGuide layoutGuide, nfloat? margin = null) =>
view.Bottom().EqualTo().BottomOf(layoutGuide).Minus(margin.GetValueOrDefault(DefaultMargin));

public static FluentLayout AtBottomOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(11, 0)
? view.Bottom().EqualTo().BottomOf(parentView.SafeAreaLayoutGuide).Minus(margin.GetValueOrDefault(DefaultMargin))
? view.AtBottomOfLayoutGuide(parentView.SafeAreaLayoutGuide, margin)
: view.AtBottomOf(parentView, margin);

public static FluentLayout AtBottomOfMargins(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(11, 0)
? view.AtBottomOfLayoutGuide(parentView.LayoutMarginsGuide, margin)
: view.AtBottomOf(parentView, margin);

public static FluentLayout AtBottomOfReadableContent(this UIView view, UIView parentView, nfloat? margin = null) =>
UIDevice.CurrentDevice.CheckSystemVersion(9, 0)
? view.AtBottomOfLayoutGuide(parentView.ReadableContentGuide, margin)
: view.AtBottomOf(parentView, margin);

public static FluentLayout AtLeadingOf(this UIView view, UIView parentView, nfloat? margin = null) =>
view.Leading().EqualTo().LeadingOf(parentView).Plus(margin.GetValueOrDefault(DefaultMargin));
Expand Down
6 changes: 5 additions & 1 deletion QuickLayout.Core/ViewModels/FirstViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MvvmCross.Logging;
using MvvmCross.Logging;
using MvvmCross.Navigation;
using MvvmCross.ViewModels;

Expand Down Expand Up @@ -35,5 +35,9 @@ public FirstViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigat
public void GoViewWithSafeArea() => NavigationService.Navigate<ViewWithSafeAreaViewModel>();

public void GoCenterConstraints() => NavigationService.Navigate<ToCenterConstraintsViewModel>();

public void GoViewWithReadableContentGuide() => NavigationService.Navigate<ViewWithReadableContentGuideViewModel>();

public void GoViewWithMarginsGuide() => NavigationService.Navigate<ViewWithMarginsGuideViewModel>();
}
}
10 changes: 10 additions & 0 deletions QuickLayout.Core/ViewModels/ViewWithMarginsGuideViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using MvvmCross.ViewModels;

namespace QuickLayout.Core.ViewModels
{

public class ViewWithMarginsGuideViewModel
: MvxViewModel
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using MvvmCross.ViewModels;

namespace QuickLayout.Core.ViewModels
{

public class ViewWithReadableContentGuideViewModel
: MvxViewModel
{
}
}
2 changes: 2 additions & 0 deletions QuickLayout.Touch/QuickLayout.Touch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
<Compile Include="Views\AdvancedVerticalStackView.cs" />
<Compile Include="Views\DirectionFormView.cs" />
<Compile Include="Views\RightToLeftView.cs" />
<Compile Include="Views\ViewWithMarginsGuideView.cs" />
<Compile Include="Views\ViewWithReadableContentGuideView.cs" />
<Compile Include="Views\ViewWithSafeAreaView.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
12 changes: 11 additions & 1 deletion QuickLayout.Touch/Views/FirstView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace QuickLayout.Touch.Views
[MvxRootPresentation(WrapInNavigationController = true)]
public class FirstView : MvxViewController
{
private UIButton _viewForm, _viewFormGrid, _viewDetails, _viewSearch, _viewTip, _viewUpdateConstaints, _viewAdvancedVerticalStack, _fullSize, _directionFormView, _rightToLeft, _viewSafeArea, _viewCenterConstraints;
private UIButton _viewForm, _viewFormGrid, _viewDetails, _viewSearch, _viewTip, _viewUpdateConstaints, _viewAdvancedVerticalStack, _fullSize, _directionFormView, _rightToLeft, _viewSafeArea, _viewCenterConstraints, _viewReadableContentGuide, _viewMarginsGuide;

public override void ViewDidLoad()
{
Expand Down Expand Up @@ -71,6 +71,14 @@ public override void ViewDidLoad()
_viewCenterConstraints = new UIButton(UIButtonType.RoundedRect);
_viewCenterConstraints.SetTitle("View Contraining to centers", UIControlState.Normal);
Add(_viewCenterConstraints);

_viewReadableContentGuide = new UIButton(UIButtonType.RoundedRect);
_viewReadableContentGuide.SetTitle("View with Readable Content Guide", UIControlState.Normal);
Add(_viewReadableContentGuide);

_viewMarginsGuide = new UIButton(UIButtonType.RoundedRect);
_viewMarginsGuide.SetTitle("View with Margins Guide", UIControlState.Normal);
Add(_viewMarginsGuide);

View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();

Expand All @@ -87,6 +95,8 @@ public override void ViewDidLoad()
set.Bind(_rightToLeft).To("GoRightToLeft");
set.Bind(_viewSafeArea).To("GoViewWithSafeArea");
set.Bind(_viewCenterConstraints).To("GoCenterConstraints");
set.Bind(_viewReadableContentGuide).To("GoViewWithReadableContentGuide");
set.Bind(_viewMarginsGuide).To("GoViewWithMarginsGuide");
set.Apply();

var constraints = View.VerticalStackPanelConstraints(
Expand Down
56 changes: 56 additions & 0 deletions QuickLayout.Touch/Views/ViewWithMarginsGuideView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Cirrious.FluentLayouts.Touch;
using Foundation;
using MvvmCross.Platforms.Ios.Views;
using UIKit;

namespace QuickLayout.Touch.Views
{
[Register("ViewWithMarginsGuideView")]
public class ViewWithMarginsGuideView : MvxViewController
{
public override void ViewDidLoad()
{
base.ViewDidLoad();

View.BackgroundColor = UIColor.FromRGB(248, 191, 120);

var viewContainer = new UIView {BackgroundColor = UIColor.Clear};

var sampleText = new UITextView
{
Editable = false,
Selectable = false,
Text =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam aliquet massa eu tempus semper. Nulla elementum mi quis erat blandit eleifend. Quisque dictum, sem eget volutpat scelerisque, quam orci lobortis enim, ut luctus enim massa in nisi. Mauris sed mi id leo lacinia lobortis. Integer elementum, erat gravida vestibulum rhoncus, enim velit consectetur est, dignissim condimentum urna turpis ac ex. Nulla arcu mauris, hendrerit nec tortor in, feugiat ullamcorper mauris. Aliquam eget tempus eros. Curabitur suscipit, arcu eu luctus mollis, nunc erat ornare erat, id viverra nisl ligula sit amet ex. Etiam in quam vitae est convallis eleifend. Fusce gravida arcu in orci lobortis pulvinar. Morbi tortor mi, elementum nec purus quis, eleifend imperdiet mi. Nunc commodo et sem vitae finibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc nec erat eget orci malesuada vulputate. \n \nPhasellus vulputate dolor eu massa varius efficitur. Sed sed tortor eu elit ornare mollis id non sapien. Nam at turpis volutpat ligula aliquam aliquet placerat sed purus. Phasellus magna urna, maximus vitae erat et, malesuada tincidunt velit. Mauris efficitur velit fermentum imperdiet convallis. In eget erat nisi. Curabitur ut leo sodales, consectetur lorem ut, iaculis est. Nullam ornare, justo id condimentum pharetra, enim nisl posuere ex, a fermentum justo enim sit amet dolor. Sed suscipit sapien augue, nec dignissim nisl laoreet eu. Maecenas eros enim, aliquet vitae arcu id, fermentum dignissim lorem. Etiam sit amet commodo nunc. In vitae ullamcorper velit. Integer in mauris eget erat mattis feugiat at id neque.",
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear
};
sampleText.TextContainer.LineBreakMode = UILineBreakMode.WordWrap;
sampleText.Font = UIFont.BoldSystemFontOfSize(18f);

View.AddSubview(viewContainer);
viewContainer.AddSubview(sampleText);

View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
viewContainer.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();

View.AddConstraints(
viewContainer.AtLeftOf(View),
viewContainer.AtTopOf(View),
viewContainer.AtRightOf(View),
viewContainer.AtBottomOf(View)
);

viewContainer.AddConstraints(
sampleText.AtTopOfMargins(viewContainer),
// sampleText.AtTopOfSafeArea(viewContainer), -> use this when UINavigationBar not present to avoid content overlap under iPhone X notch
sampleText.AtLeftOfMargins(viewContainer),
//sampleText.AtLeftOf(viewContainer), -> if this is used, content overlaps under notch on landscape
sampleText.AtRightOfMargins(viewContainer),
//sampleText.AtRightOf(viewContainer), -> if this is used, content overlaps under notch on landscape
sampleText.AtBottomOfMargins(viewContainer)
//sampleText.AtBottomOf(viewContainer) -> if this is used, content overlaps under notch on landscape
);
}
}
}
56 changes: 56 additions & 0 deletions QuickLayout.Touch/Views/ViewWithReadableContentGuideView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Cirrious.FluentLayouts.Touch;
using Foundation;
using MvvmCross.Platforms.Ios.Views;
using UIKit;

namespace QuickLayout.Touch.Views
{
[Register("ViewWithReadableContentGuideView")]
public class ViewWithReadableContentGuideView : MvxViewController
{
public override void ViewDidLoad()
{
base.ViewDidLoad();

View.BackgroundColor = UIColor.FromRGB(248, 191, 120);

var viewContainer = new UIView {BackgroundColor = UIColor.Clear};

var sampleText = new UITextView
{
Editable = false,
Selectable = false,
Text =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam aliquet massa eu tempus semper. Nulla elementum mi quis erat blandit eleifend. Quisque dictum, sem eget volutpat scelerisque, quam orci lobortis enim, ut luctus enim massa in nisi. Mauris sed mi id leo lacinia lobortis. Integer elementum, erat gravida vestibulum rhoncus, enim velit consectetur est, dignissim condimentum urna turpis ac ex. Nulla arcu mauris, hendrerit nec tortor in, feugiat ullamcorper mauris. Aliquam eget tempus eros. Curabitur suscipit, arcu eu luctus mollis, nunc erat ornare erat, id viverra nisl ligula sit amet ex. Etiam in quam vitae est convallis eleifend. Fusce gravida arcu in orci lobortis pulvinar. Morbi tortor mi, elementum nec purus quis, eleifend imperdiet mi. Nunc commodo et sem vitae finibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc nec erat eget orci malesuada vulputate. \n \nPhasellus vulputate dolor eu massa varius efficitur. Sed sed tortor eu elit ornare mollis id non sapien. Nam at turpis volutpat ligula aliquam aliquet placerat sed purus. Phasellus magna urna, maximus vitae erat et, malesuada tincidunt velit. Mauris efficitur velit fermentum imperdiet convallis. In eget erat nisi. Curabitur ut leo sodales, consectetur lorem ut, iaculis est. Nullam ornare, justo id condimentum pharetra, enim nisl posuere ex, a fermentum justo enim sit amet dolor. Sed suscipit sapien augue, nec dignissim nisl laoreet eu. Maecenas eros enim, aliquet vitae arcu id, fermentum dignissim lorem. Etiam sit amet commodo nunc. In vitae ullamcorper velit. Integer in mauris eget erat mattis feugiat at id neque.",
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear
};
sampleText.TextContainer.LineBreakMode = UILineBreakMode.WordWrap;
sampleText.Font = UIFont.BoldSystemFontOfSize(18f);

View.AddSubview(viewContainer);
viewContainer.AddSubview(sampleText);

View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
viewContainer.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();

View.AddConstraints(
viewContainer.AtLeftOf(View),
viewContainer.AtTopOf(View),
viewContainer.AtRightOf(View),
viewContainer.AtBottomOf(View)
);

viewContainer.AddConstraints(
sampleText.AtTopOfReadableContent(viewContainer),
// sampleText.AtTopOfSafeArea(viewContainer), -> use this when UINavigationBar not present to avoid content overlap under iPhone X notch
sampleText.AtLeftOfReadableContent(viewContainer),
//sampleText.AtLeftOf(viewContainer), -> if this is used, content overlaps under notch on landscape
sampleText.AtRightOfReadableContent(viewContainer),
//sampleText.AtRightOf(viewContainer), -> if this is used, content overlaps under notch on landscape
sampleText.AtBottomOfSafeArea(viewContainer)
//sampleText.AtBottomOf(viewContainer) -> if this is used, content overlaps under notch on landscape
);
}
}
}