I'm looking to add a bottom sheet to my Xamarin.iOS app and I'm referencing this SO Answer and I'm running into some problems.
first off when I click on the button that's meant to trigger the bottom sheet, it comes up but not nearly high enough, I'd like it to come up at least until the content is visible.
my other issue is not really explained in the SO answer I'm referencing, but I'd like to add a way to have the bottom sheet close if the user taps out of it, but I don't know how to do that.
here is my bottom sheet code.
public partial class BottomSheetViewController : UIViewController
{
private nfloat PartialView => UIScreen.MainScreen.Bounds.Height - lblText.Frame.GetMaxY() + UIApplication.SharedApplication.StatusBarFrame.Height;
private nfloat FullView => 300;
public BottomSheetViewController() : base("BottomSheetViewController", null)
{
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
PrepareBackground();
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
UIView.Animate(0.6, () =>
{
var frame = View.Frame;
var yComponent = PartialView;
View.Frame = new CGRect(0, yComponent, frame.Width, frame.Height);
});
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var gesture = new UIPanGestureRecognizer((g) => PanGesture(g));
View.AddGestureRecognizer(gesture);
RoundView();
}
private void RoundView()
{
View.Layer.CornerRadius = 5;
View.ClipsToBounds = true;
}
private void PrepareBackground()
{
var blurEffect = UIBlurEffect.FromStyle(UIBlurEffectStyle.Dark);
var visualEffect = new UIVisualEffectView(blurEffect);
var bluredView = new UIVisualEffectView(blurEffect);
bluredView.ContentView.AddSubview(visualEffect);
visualEffect.Frame = UIScreen.MainScreen.Bounds;
bluredView.Frame = UIScreen.MainScreen.Bounds;
View.InsertSubview(bluredView, 0);
}
private void PanGesture(UIPanGestureRecognizer recognizer)
{
var translation = recognizer.TranslationInView(View);
var velocity = recognizer.VelocityInView(View);
var y = View.Frame.GetMinY();
if (y + translation.Y >= FullView && y + translation.Y <= PartialView)
{
View.Frame = new CGRect(0, y + translation.Y, View.Frame.Width, View.Frame.Height);
recognizer.SetTranslation(CGPoint.Empty, View);
}
if (recognizer.State == UIGestureRecognizerState.Ended)
{
var duration = velocity.Y < 0 ? Convert.ToDouble(y - FullView / -velocity.Y) : Convert.ToDouble(PartialView - y / velocity.Y);
duration = duration > 1.3 ? 1 : duration;
UIView.Animate(duration, 0.0, UIViewAnimationOptions.AllowAnimatedContent, () =>
{
if (velocity.Y >= 0)
{
View.Frame = new CGRect(0, PartialView, View.Frame.Width, View.Frame.Height);
}
else
{
View.Frame = new CGRect(0, FullView, View.Frame.Width, View.Frame.Height);
}
}, null);
}
}
}
if you're curious how I'm presenting the bottom sheet:
var bottomSheet = new BottomSheetViewController();
AddChildViewController(bottomSheet);
View.AddSubview(bottomSheet.View);
bottomSheet.DidMoveToParentViewController(this);
var height = View.Frame.Height;
var width = View.Frame.Width;
bottomSheet.View.Frame = new CGRect(0, View.Frame.GetMaxY(), width, height);
the bottom sheet is otherwise working well, just need a few things ironed out and I need some help figuring out what I'm missing.