0

I have a custom UIView that's my little component lets call it PlaceholderView.

My UITableViewCell prototype has a Label and a PlaceholderView that sits inside a UIStackView that's vertically axis.

The PlaceholderView, is supposed to call some custom code that goes to a cache to retrieve a specific view then it adds it to the SubView of the PlaceholderView.

I want this subview to take up the whole surface of the entire PlaceholderView. How do I go about doing that? I tried this but not sure if it does the job

if (view != null)
{
    AddSubview(view);
    view.SizeToFit();
}

Second question. These view's that I am adding, when I create them during design time, I make a new storyboard, drag and drop a ViewController then proceed to place other controls like Labels and Button's on it.

How do I restrict this ViewController's overall height so it's completely fixed size? I know I can set the simulated metrics, and I am also setting the View. Frame's size to restrict the height.

Are there better ways to make these views constrained easier?

Currently, when I am setting these fixed height values, it does cause some weird issues with overlaps if I set UITableView.RowHeigh to AutomaticDimension.

Bhavik Modi
  • 1,517
  • 14
  • 29
wpa
  • 220
  • 3
  • 11

2 Answers2

0
// attaches all sides of the child to its parent view
extension UIView {    
    func layoutToSuperview() {
        guard let view = self.superview else {return}
        self.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        self.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        self.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        self.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    }
}

Usage: Instead of view.SizeToFit() use view.layoutToSuperview()

prex
  • 719
  • 4
  • 17
0

I want this subview to take up the whole surface of the entire PlaceholderView

You can try autolayout:

if (view != null)
{
    view.TranslatesAutoresizingMaskIntoConstraints = false;
    AddSubview(view);
    view.LeadingAnchor.ConstraintEqualTo(this.LeadingAnchor).Active = true;
    view.TopAnchor.ConstraintEqualTo(this.TopAnchor).Active = true;
    view.TrailingAnchor.ConstraintEqualTo(this.TrailingAnchor).Active = true;
    view.BottomAnchor.ConstraintEqualTo(this.BottomAnchor).Active = true;
}

Are there better ways to make these views constrained easier?

The answer is also auto layout. Set a control's height constraint to claim how much space it wants to take. See this thread for more details: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.

Though it is native oc, you can see its concept.

Ax1le
  • 6,563
  • 2
  • 14
  • 61