Forum Xamarin Xamarin.iOS

How to use NSLayoutConstraint to get a scroll view like a WPF StackPanel?

VioletaSanchezVioletaSanchez ESMember ✭✭

Hi,

I'm trying to do a scroll panel like the attached picture.

I started using a NSTableView, but I have found that "you cannot pass a custom NSObject-based class from
NSTableViewDataSource.GetObjectValue, which is extremely important when creating custom NSCells." at https://bugzilla.xamarin.com/show_bug.cgi?id=1086
I need custom panels so I discarded this option.

So I started reading about NSLayoutConstraint. If I have my controls arranging correctly amongst themselves, then I should just stick those controls into a scrollviewer.

But the problem is that I cannot get it working. I'm trying, simply, drawing two rectangles, one down the other, using NSLayoutConstraint.

   NSView foo = new NSBox (new RectangleF ()){
            BoxType = NSBoxType.NSBoxCustom,
            BorderType = NSBorderType.LineBorder,
            TitlePosition = NSTitlePosition.NoTitle,
            FillColor = NSColor.Blue
        };

   NSView bar = new NSBox (new RectangleF ()){
            BoxType = NSBoxType.NSBoxCustom,
            BorderType = NSBorderType.LineBorder,
            TitlePosition = NSTitlePosition.NoTitle,
            FillColor = NSColor.Blue
        };

    TranslatesAutoresizingMaskIntoConstraints = false;
    foo.TranslatesAutoresizingMaskIntoConstraints = false;
    bar.TranslatesAutoresizingMaskIntoConstraints = false;

    AddSubview(foo);
    AddSubview(bar);

    var views = new NSDictionary("foo", foo, "bar", bar);
    var metrics = new NSDictionary("fooHeight", 15, "barHeight", 30);
    var constraints = new List<NSLayoutConstraint>();

    // these make foo and bar's widths equal to the parent's width
    constraints.AddRange(NSLayoutConstraint.FromVisualFormat("H:|[foo]|", 0, metrics, views));
    constraints.AddRange(NSLayoutConstraint.FromVisualFormat("H:|[bar]|", 0, metrics, views));

    // put foo at the top and bar below it
    constraints.AddRange(NSLayoutConstraint.FromVisualFormat("V:|[foo(fooHeight)][bar(barHeight)]", 0, metrics, views));

    AddConstraints(constraints.ToArray());

What am I doing wrong?

Any other ways (easier) to get something like in the picture? NSTableView? NSCollectionView?

Sign In or Register to comment.