Advertisement
Don_Mag

Untitled

May 4th, 2020
1,741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.31 KB | None | 0 0
  1. /*
  2. First tip: Get rid of this function...
  3.  
  4.    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)
  5.  
  6. It only saves a few keystrokes, and causes problems if/when we need change constraints (the Constant, Priority, Activate/Deactivate, etc).
  7.  
  8. A better approach than adding a "Container" view and setting constraints on individual CardViews would be to use a Stack View.
  9.  
  10. In plain language...
  11.  
  12. 1. Add scrollview to VC view
  13. 2. constrain all 4 sides to safe area
  14. 3. add a UIStackView to scroll view
  15. 4. constrain all 4 sides to scrollView.contentLayoutGuide (10-pts padding)
  16. 5. constrain stack view width to scrollView.frameLayoutGuide.widthAnchor -20 (10-pts on each side
  17. 6. create CardViews and add as arranged subviews of stackView
  18.  
  19. - on pull-to-refresh, either
  20.  - update the content of the current CardViews
  21. - or
  22.  - remove each cardView from superView, and re-add new ones
  23.  
  24. So, if I understand the Xamarin / C# syntax correctly (there may be an error or two in here):
  25. */
  26.  
  27. public override void ViewDidLoad()
  28. {
  29.     base.ViewDidLoad();
  30.  
  31.     _scrollView = new UIScrollView
  32.     {
  33.         ShowsHorizontalScrollIndicator = false,
  34.         TranslatesAutoresizingMaskIntoConstraints = false
  35.     };
  36.  
  37.     if (!UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
  38.     {
  39.         _scrollView.AlwaysBounceVertical = true;
  40.         _scrollView.Bounces = true;
  41.     }
  42.  
  43.     _refreshControl = new UIRefreshControl { TranslatesAutoresizingMaskIntoConstraints = false };
  44.     _refreshControl.Enabled = true;
  45.     _refreshControl.ValueChanged -= RefreshControl_ValueChanged;
  46.     _refreshControl.ValueChanged += RefreshControl_ValueChanged;
  47.     if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
  48.     {
  49.         _scrollView.RefreshControl = _refreshControl;
  50.     }
  51.     else
  52.     {
  53.         _scrollView.AddSubview(_refreshControl);
  54.     }
  55.  
  56. // 1. Add scrollview to VC view
  57.     this.View.AddSubview(_scrollView);
  58.  
  59. // 2. constrain all 4 sides to safe area
  60.     _scrollView.TopAnchor.ConstraintEqualTo(this.View.SaferAreaLayoutGuide().TopAnchor, 0f).Active = true;
  61.     _scrollView.LeadingAnchor.ConstraintEqualTo(this.View.SaferAreaLayoutGuide().LeadingAnchor, 0f).Active = true;
  62.     _scrollView.TrailingAnchor.ConstraintEqualTo(this.View.SaferAreaLayoutGuide().TrailingAnchor, 0f).Active = true;
  63.     _scrollView.BottomAnchor.ConstraintEqualTo(this.View.SaferAreaLayoutGuide().BottomAnchor, 0f).Active = true;
  64.  
  65. // 3. add a UIStackView to scroll view
  66.     _stackView = new UIStackView {
  67.         TranslatesAutoresizingMaskIntoConstraints = false
  68.         Axis = UILayoutConstraintAxis.Vertical
  69.         Distribution = UIStackViewDistribution.Fill
  70.         Spacing = 10f
  71.     };
  72.  
  73.     _scrollView.AddSubview(_stackView);
  74.  
  75. // 4. constrain all 4 sides to scrollView.contentLayoutGuide (10-pts padding)
  76.     _stackView.TopAnchor.ConstraintEqualTo(_scrollView.ContentLayoutGuide.TopAnchor, 10f).Active = true;
  77.     _stackView.LeadingAnchor.ConstraintEqualTo(_scrollView.ContentLayoutGuide.LeadingAnchor, 10f).Active = true;
  78.     _stackView.TrailingAnchor.ConstraintEqualTo(_scrollView.ContentLayoutGuide.TrailingAnchor, -10f).Active = true;
  79.     _stackView.BottomAnchor.ConstraintEqualTo(_scrollView.ContentLayoutGuide.BottomAnchor, -10f).Active = true;
  80.  
  81. // 5. constrain stack view width to scrollView.frameLayoutGuide.widthAnchor -20 (10-pts on each side
  82.     _stackView.WidthAnchor.ConstraintEqualTo(_scrollView.FrameLayoutGuide.WidthAnchor, -20f).Active = true;
  83.  
  84.     // NO Height constraint for stackView
  85.  
  86. // 6. create CardViews and add as arranged subviews of stackView
  87.     CardView qolCard = new CardView();
  88.     _stackView.AddArrangedSubview(qolCard);
  89.     qolCard.TranslatesAutoresizingMaskIntoConstraints = false;
  90.     qolCard.CornerRadius = 5f;
  91.     qolCard.ShadowOffsetHeight = 0;
  92.     qolCard.ShadowOffsetWidth = 0;
  93.     qolCard.BackgroundColor = UIColor.White;
  94.  
  95.     CardView goalsCard = new CardView();
  96.     _stackView.AddArrangedSubview(goalsCard);
  97.     goalsCard.TranslatesAutoresizingMaskIntoConstraints = false;
  98.     goalsCard.CornerRadius = 5f;
  99.     goalsCard.ShadowOffsetHeight = 0;
  100.     goalsCard.ShadowOffsetWidth = 0;
  101.     goalsCard.BackgroundColor = UIColor.White;
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement