I'm trying to create a ViewController in Xamarin.iOS to show a modal page that scales height as needed, and when not enough height is available it will scroll the content.
Anyone suggestions how to set the UIScrollView to do this?
Currently I can show
- a height scaled View that is Y centered (showSmallDesign = true)
- and I can show a View that uses max height that can scroll (showSmallDesign = false)
But I can’t combine those two behaviours for some reason; creating a scroll view that expands height until parent height and enabling the scroll. I have tried a lot of different constraints but it wont work.
This is my UIController (copy paste and it should work)
public class DialogViewModelBaseController : UIViewController
{
private readonly bool showSmallDesign = false;
public override void ViewDidLoad()
{
///
/// Create Views
///
var brownOuterView = new UIView {BackgroundColor = UIColor.Brown};
brownOuterView.Layer.CornerRadius = 20;
View.Add(brownOuterView);
var greenScrollView = showSmallDesign
? new UIView { BackgroundColor = UIColor.Green }
: new UIScrollView() { BackgroundColor = UIColor.Green };
brownOuterView.Add(greenScrollView);
var cyanInnerView = new UIView {BackgroundColor = UIColor.Cyan};
greenScrollView.Add(cyanInnerView);
var redLabel = new UILabel
{
LineBreakMode = UILineBreakMode.WordWrap,
Lines = 0,
BackgroundColor = UIColor.Red
};
redLabel.Text = showSmallDesign
? "Lorem ipsum"
: "Lorem ipsum dolor sit amet consectetur adipiscing elit. Aenean id lorem at tellus euismod gravida. Morbi scelerisque molestie nulla, pulvinar congue neque viverra ac. Donec euismod bibendum lectus eget ultrices. Ut eu vehicula lectus. In est orci, feugiat a sollicitudin eu, posuere ac sem. Quisque eu egestas lectus. Aliquam rhoncus dictum nisl a ullamcorper. Donec eget fermentum purus. Proin accumsan aliquam lacus ut convallis. Etiam tincidunt vitae nunc non sodales. Cras vitae nunc mattis, ultricies turpis et, laoreet nulla. Duis blandit sit amet libero ac cursus. Praesent ultricies erat laoreet turpis placerat scelerisque. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean id lorem at tellus euismod gravida. Morbi scelerisque molestie nulla, pulvinar congue neque viverra ac. Donec euismod bibendum lectus eget ultrices. Ut eu vehicula lectus. In est orci, feugiat a sollicitudin eu, posuere ac sem. Quisque eu egestas lectus. Aliquam rhoncus dictum nisl a ullamcorper. Donec eget fermentum purus. Proin accumsan aliquam lacus ut convallis. Etiam tincidunt vitae nunc non sodales. Cras vitae nunc mattis, ultricies turpis et, laoreet nulla. Duis blandit sit amet libero ac cursus. Praesent ultricies erat laoreet turpis placerat scelerisque. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean id lorem at tellus euismod gravida. Morbi scelerisque molestie nulla, pulvinar congue neque viverra ac. Donec euismod bibendum lectus eget ultrices. Ut eu vehicula lectus. In est orci, feugiat a sollicitudin eu, posuere ac sem. Quisque eu egestas lectus. Aliquam rhoncus dictum nisl a ullamcorper. Donec eget fermentum purus. Proin accumsan aliquam lacus ut convallis. Etiam tincidunt vitae nunc non sodales. Cras vitae nunc mattis, ultricies turpis et, laoreet nulla. Duis blandit sit amet libero ac cursus. Praesent ultricies erat laoreet turpis placerat scelerisque. XXXX";
cyanInnerView.Add(redLabel);
var blueButton = new UIButton {BackgroundColor = UIColor.Blue};
blueButton.SetTitle(" OK ", UIControlState.Normal);
blueButton.TouchUpInside += (sender, args) =>
{
DismissViewController(true, null);
};
cyanInnerView.Add(blueButton);
var outerMargin = 10;
var innerMargin = 16;
///
/// Layout Views
///
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.AddConstraints(
brownOuterView.AtRightOfSafeArea(View, outerMargin),
brownOuterView.AtLeftOfSafeArea(View, outerMargin)
);
if (showSmallDesign)
{
View.AddConstraints(
// works for none scroll subview (small resizable height)
brownOuterView.WithSameCenterY(View),
brownOuterView.Height().LessThanOrEqualTo().HeightOf(View.SafeAreaLayoutGuide).Minus(2 * outerMargin) // this resizes height to max View size
);
}
else
{
View.AddConstraints(
// works for scroll subview (maximum height)
brownOuterView.AtTopOfSafeArea(View, outerMargin),
brownOuterView.AtBottomOfSafeArea(View, outerMargin)
);
}
brownOuterView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
brownOuterView.AddConstraints(
greenScrollView.AtRightOf(brownOuterView, outerMargin),
greenScrollView.AtLeftOf(brownOuterView, outerMargin),
greenScrollView.AtTopOf(brownOuterView, outerMargin),
greenScrollView.AtBottomOf(brownOuterView, outerMargin)
);
greenScrollView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
greenScrollView.AddConstraints(
cyanInnerView.AtTopOf(greenScrollView, innerMargin),
cyanInnerView.AtLeftOf(greenScrollView, innerMargin),
cyanInnerView.Width().EqualTo().WidthOf(greenScrollView).Minus(2 * innerMargin),
cyanInnerView.Bottom().EqualTo().BottomOf(greenScrollView).Minus((nfloat)(innerMargin)) // constraint on last item to let scroll work
);
cyanInnerView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
cyanInnerView.AddConstraints(
redLabel.WithSameWidth(cyanInnerView),
redLabel.AtTopOf(cyanInnerView),
blueButton.Below(redLabel, 20),
blueButton.Bottom().EqualTo().BottomOf(cyanInnerView).Minus((nfloat)(20)) // constraint on last item to let scroll work
);
}
}
To show the Controller as Modal, just use something like:
var dialogVC = new DialogViewModelBaseController()
{
ModalPresentationStyle = (DeviceInfo.IsPhone) ? UIModalPresentationStyle.OverCurrentContext : UIModalPresentationStyle.FormSheet,
ModalTransitionStyle = UIModalTransitionStyle.CoverVertical
};
var vc = CrossCurrentViewController.GetTopViewController();
vc.PresentViewController(dialogVC, true, null);