Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- First tip: Get rid of this function...
- internal static void Anchor(this UIView uIView, NSLayoutYAxisAnchor top = null, NSLayoutXAxisAnchor leading = null, NSLayoutYAxisAnchor bottom = null, NSLayoutXAxisAnchor trailing = null, UIEdgeInsets padding = default, CGSize size = default)
- It only saves a few keystrokes, and causes problems if/when we need change constraints (the Constant, Priority, Activate/Deactivate, etc).
- A better approach than adding a "Container" view and setting constraints on individual CardViews would be to use a Stack View.
- In plain language...
- 1. Add scrollview to VC view
- 2. constrain all 4 sides to safe area
- 3. add a UIStackView to scroll view
- 4. constrain all 4 sides to scrollView.contentLayoutGuide (10-pts padding)
- 5. constrain stack view width to scrollView.frameLayoutGuide.widthAnchor -20 (10-pts on each side
- 6. create CardViews and add as arranged subviews of stackView
- - on pull-to-refresh, either
- - update the content of the current CardViews
- - or
- - remove each cardView from superView, and re-add new ones
- So, if I understand the Xamarin / C# syntax correctly (there may be an error or two in here):
- */
- public override void ViewDidLoad()
- {
- base.ViewDidLoad();
- _scrollView = new UIScrollView
- {
- ShowsHorizontalScrollIndicator = false,
- TranslatesAutoresizingMaskIntoConstraints = false
- };
- if (!UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
- {
- _scrollView.AlwaysBounceVertical = true;
- _scrollView.Bounces = true;
- }
- _refreshControl = new UIRefreshControl { TranslatesAutoresizingMaskIntoConstraints = false };
- _refreshControl.Enabled = true;
- _refreshControl.ValueChanged -= RefreshControl_ValueChanged;
- _refreshControl.ValueChanged += RefreshControl_ValueChanged;
- if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
- {
- _scrollView.RefreshControl = _refreshControl;
- }
- else
- {
- _scrollView.AddSubview(_refreshControl);
- }
- // 1. Add scrollview to VC view
- this.View.AddSubview(_scrollView);
- // 2. constrain all 4 sides to safe area
- _scrollView.TopAnchor.ConstraintEqualTo(this.View.SaferAreaLayoutGuide().TopAnchor, 0f).Active = true;
- _scrollView.LeadingAnchor.ConstraintEqualTo(this.View.SaferAreaLayoutGuide().LeadingAnchor, 0f).Active = true;
- _scrollView.TrailingAnchor.ConstraintEqualTo(this.View.SaferAreaLayoutGuide().TrailingAnchor, 0f).Active = true;
- _scrollView.BottomAnchor.ConstraintEqualTo(this.View.SaferAreaLayoutGuide().BottomAnchor, 0f).Active = true;
- // 3. add a UIStackView to scroll view
- _stackView = new UIStackView {
- TranslatesAutoresizingMaskIntoConstraints = false
- Axis = UILayoutConstraintAxis.Vertical
- Distribution = UIStackViewDistribution.Fill
- Spacing = 10f
- };
- _scrollView.AddSubview(_stackView);
- // 4. constrain all 4 sides to scrollView.contentLayoutGuide (10-pts padding)
- _stackView.TopAnchor.ConstraintEqualTo(_scrollView.ContentLayoutGuide.TopAnchor, 10f).Active = true;
- _stackView.LeadingAnchor.ConstraintEqualTo(_scrollView.ContentLayoutGuide.LeadingAnchor, 10f).Active = true;
- _stackView.TrailingAnchor.ConstraintEqualTo(_scrollView.ContentLayoutGuide.TrailingAnchor, -10f).Active = true;
- _stackView.BottomAnchor.ConstraintEqualTo(_scrollView.ContentLayoutGuide.BottomAnchor, -10f).Active = true;
- // 5. constrain stack view width to scrollView.frameLayoutGuide.widthAnchor -20 (10-pts on each side
- _stackView.WidthAnchor.ConstraintEqualTo(_scrollView.FrameLayoutGuide.WidthAnchor, -20f).Active = true;
- // NO Height constraint for stackView
- // 6. create CardViews and add as arranged subviews of stackView
- CardView qolCard = new CardView();
- _stackView.AddArrangedSubview(qolCard);
- qolCard.TranslatesAutoresizingMaskIntoConstraints = false;
- qolCard.CornerRadius = 5f;
- qolCard.ShadowOffsetHeight = 0;
- qolCard.ShadowOffsetWidth = 0;
- qolCard.BackgroundColor = UIColor.White;
- CardView goalsCard = new CardView();
- _stackView.AddArrangedSubview(goalsCard);
- goalsCard.TranslatesAutoresizingMaskIntoConstraints = false;
- goalsCard.CornerRadius = 5f;
- goalsCard.ShadowOffsetHeight = 0;
- goalsCard.ShadowOffsetWidth = 0;
- goalsCard.BackgroundColor = UIColor.White;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement