public static void AddSubviewDockBottom(this UIView containerView, UIView subView, UIEdgeInsets?insets = null) { containerView.AddSubviewDock( subView, insets, DockTarget.ToContainer(), null, DockTarget.ToContainer(), DockTarget.ToContainer() ); }
public static void AddSubviewDock(this UIView containerView, UIView subView, UIEdgeInsets?insets = null, DockTarget leftTarget = null, DockTarget topTarget = null, DockTarget rightTarget = null, DockTarget bottomTarget = null) { if (insets == null) { insets = UIEdgeInsets.Zero; } subView.TranslatesAutoresizingMaskIntoConstraints = false; containerView.AddSubview(subView); NSLayoutConstraint leftConstraint = null, topConstraint = null, rightConstraint = null, bottomConstraint = null; if (leftTarget != null) { switch (leftTarget.TargetType) { case DockTargetType.DockToContainer: leftConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, containerView, NSLayoutAttribute.Left, 1f, insets.Value.Left); break; case DockTargetType.DockToView: leftConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, leftTarget.View, NSLayoutAttribute.Right, 1.0f, insets.Value.Left); break; case DockTargetType.FixedLength: leftConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, containerView, NSLayoutAttribute.Left, 1.0f, leftTarget.FixedLength + insets.Value.Left); break; case DockTargetType.PercentageOfContainer: throw new NotSupportedException("PercentageOfContainer is not valid for left dock target"); break; } } if (topTarget != null) { switch (topTarget.TargetType) { case DockTargetType.DockToContainer: topConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, containerView, NSLayoutAttribute.Top, 1f, insets.Value.Top); break; case DockTargetType.DockToView: topConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, topTarget.View, NSLayoutAttribute.Bottom, 1.0f, insets.Value.Top); break; case DockTargetType.FixedLength: topConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, containerView, NSLayoutAttribute.Top, 1f, topTarget.FixedLength + insets.Value.Top); break; case DockTargetType.PercentageOfContainer: throw new NotSupportedException("PercentageOfContainer is not valid for top dock target"); break; } } if (rightTarget != null) { switch (rightTarget.TargetType) { case DockTargetType.DockToContainer: rightConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, containerView, NSLayoutAttribute.Right, 1f, -insets.Value.Right); break; case DockTargetType.DockToView: rightConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, rightTarget.View, NSLayoutAttribute.Left, 1.0f, -insets.Value.Right); break; case DockTargetType.FixedLength: rightConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, rightTarget.FixedLength - insets.Value.Right); break; case DockTargetType.PercentageOfContainer: rightConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, containerView, NSLayoutAttribute.Width, rightTarget.Percentage, -insets.Value.Right); break; } } if (bottomTarget != null) { switch (bottomTarget.TargetType) { case DockTargetType.DockToContainer: bottomConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, containerView, NSLayoutAttribute.Bottom, 1f, -insets.Value.Bottom); break; case DockTargetType.DockToView: bottomConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, bottomTarget.View, NSLayoutAttribute.Top, 1.0f, -insets.Value.Bottom); break; case DockTargetType.FixedLength: bottomConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, bottomTarget.FixedLength - insets.Value.Bottom); break; case DockTargetType.PercentageOfContainer: bottomConstraint = NSLayoutConstraint.Create(subView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, containerView, NSLayoutAttribute.Height, bottomTarget.Percentage, -insets.Value.Bottom); break; } } var constraints = new[] { leftConstraint, topConstraint, rightConstraint, bottomConstraint }.Where(c => c != null).ToArray(); if (constraints.Any()) { containerView.AddConstraints(constraints); } }
public MenuCell(CGRect frame) : base(frame) { this.BackgroundColor = UIColor.Clear; BackgroundView = new UIView { BackgroundColor = UIColor.Clear }; //SelectedBackgroundView = new UIView { BackgroundColor = UIColor.Red }; //this.Layer.BorderColor = UIColor.Brown.CGColor; //this.Layer.BorderWidth = 1; ContentView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight; ContentView.Frame = ContentView.Bounds; //ContentView.Layer.BorderColor = UIColor.LightGray.CGColor; //ContentView.Layer.BorderWidth = 1.0f; ContentView.BackgroundColor = UIColor.Clear; //ContentView.Layer.CornerRadius = 5.0f; ContentView.Layer.MasksToBounds = true; _imageView = new UIImageView(/*UIImage.FromBundle("placeholder.png")*/); _imageView.Bounds = ContentView.Bounds; _imageView.Center = ContentView.Center; _imageView.ContentMode = UIViewContentMode.ScaleAspectFit; var imageContainerView = new UIView(); imageContainerView.AddSubviewDockFull(_imageView, new UIEdgeInsets(5, 5, 5, 5)); imageContainerView.BackgroundColor = UIColor.Clear; imageContainerView.Layer.BorderWidth = 2.0f; imageContainerView.Layer.BorderColor = this.TintColor.CGColor; imageContainerView.Layer.CornerRadius = 10.0f; ContentView.AddSubviewDock(imageContainerView, new UIEdgeInsets(0, 0, 0, 0), DockTarget.ToContainer(), DockTarget.ToContainer(), DockTarget.ToFixedLength(ImageWidthHeight), DockTarget.ToFixedLength(ImageWidthHeight /*+20*/)); // create label _labelView = new UILabel(ContentView.Bounds); _labelView.TextColor = UIColor.Black; _labelView.BackgroundColor = UIColor.Clear; _labelView.Text = "SiteName"; _labelView.Font = _labelView.Font.WithSize(10); _labelView.Lines = 2; _labelView.TextAlignment = UITextAlignment.Center; //_labelView.Layer.BorderWidth = 1; //_labelView.Layer.BorderColor = UIColor.Black.CGColor; ContentView.AddSubviewDock(_labelView, new UIEdgeInsets(3, 0, 0, 0), leftTarget: DockTarget.ToContainer(), topTarget: DockTarget.ToView(imageContainerView), rightTarget: DockTarget.ToContainer()); }