0

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.

Eman
  • 1,093
  • 2
  • 26
  • 49

1 Answers1

1

About first question , you can set parameter FullView of BottomSheetViewController to be 44 .

public partial class BottomSheetViewController : UIViewController
{
    private nfloat PartialView => UIScreen.MainScreen.Bounds.Height - lblText.Frame.GetMaxY() + UIApplication.SharedApplication.StatusBarFrame.Height;
    private nfloat FullView => 44;

    ...

    public override void ViewDidLayoutSubviews()
    {
        base.ViewDidLayoutSubviews();
        RoundView();
        //move RoundView from ViewDidLoad to ViewDidLayoutSubviews , will hold on the round corner effect
    }

}

Then it will show full screen . If that's not your want , you can share an image to explain that .

The second one , you can add a UITapGestureRecognizer in current View Controller to achieve that :

BottomSheetViewController bottomSheet;

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    UITapGestureRecognizer tapGestureRecognizer = new UITapGestureRecognizer(TapMethod);
    tapGestureRecognizer.NumberOfTapsRequired = 1;
    View.AddGestureRecognizer(tapGestureRecognizer);

}

private void TapMethod(UITapGestureRecognizer sender)
{
    Console.WriteLine("Tap--");

    if (sender.State == UIGestureRecognizerState.Ended)
    {
        UIView.Animate(1, 0.0, UIViewAnimationOptions.AllowAnimatedContent, () =>
        {
            bottomSheet.View.Frame = new CGRect(0, UIScreen.MainScreen.Bounds.Size.Height, bottomSheet.View.Frame.Width, bottomSheet.View.Frame.Height);
        }, () => {
            CGPoint point = sender.LocationInView(null);
            if (!bottomSheet.View.PointInside(secondViewController.View.ConvertPointFromView(point, View.Window), null))
            {
                Console.WriteLine("remove--");
                bottomSheet.View.RemoveFromSuperview();
                bottomSheet.RemoveFromParentViewController();
            }
        });
    }
}

Then it will disapper when touch other area of Current View controller .

enter image description here

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • setting it to 44 didn't work, but I think I saw my problem, I only had a label in the bottom sheet and after I added a tableview, which is what I was going to have in there anyways, the sheet got bigger. Thanks for the help. – Eman Apr 22 '20 at 14:54
  • @Eman Glad solved ! Yeah , the size of `FullView` depends on inside views , here I set a default value . Thanks for marking. :-) – Junior Jiang Apr 23 '20 at 01:17