Xamarin Ios中的浮动操作按钮

时间:2019-09-17 22:07:44

标签: c# xamarin xamarin.ios

我正在尝试以编程方式在Xamarin Ios中实现FAB。

到目前为止,这就是我所拥有的:

public class UIFAB : UIButton
    {
        public string BackgroundImage{ set => this.SetImage(UIImage.FromBundle(value), UIControlState.Normal); }
        public UIFAB()
        {
            Layer.CornerRadius = 24;
            Layer.MasksToBounds = true;
            Layer.ShadowOpacity = 0.25f;
            Layer.ShadowRadius = 5f;
            Layer.ShadowOffset = new CGSize(0, 10);
            Layer.BackgroundColor = UIColor.FromRGB(90,40,0).CGColor;
        }
    }

public class FABContainer : UIView
    {
        public List<UIFAB> Buttons{ get; set; } //For when i need more than one action
        public UIFAB Main { get; set; }

        public FABContainer(CGRect frame) : base(frame)
        {
            Layer.ZPosition = 1;
            BackgroundColor = UIColor.Clear;

            AutoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleTopMargin;

            Main = new UIFAB
            {
                BackgroundImage = "action_menu",
                Frame = new CGRect(0, 0, 50, 50)
            };

            Main.TouchUpInside += (s, e) =>
            {
                Animate(.3, () =>
                {
                    Main.Transform.Rotate((float)(45 * Math.PI / 180));
                });
                if (Main.Transform.IsIdentity)
                {
                }
                else
                {
                    Main.Transform = CGAffineTransform.MakeIdentity();
                }
            };

            AddSubview(Main);
        }
    }

在将“ FABContainer”添加到控制器的子视图之后,这可以正确地将其添加到页面底部,我可以使用大小和图像,但是存在以下严重问题:

  1. 使其可调整大小以用于其他手机(已在XR和6上测试)
  2. 在添加到滚动视图时使其跟随滚动

需要一些启示

编辑: 尽管我试图实现一个没有库来完全控制行为的手动解决方案,但这被标记为重复项。这就是我最终要做的事情:

未修改UIButton或其容器中的任何内容以供将来使用。我必须转到控制器级别并重写Scrolled(或在本机中为scrolViewDidScroll)方法,并放置此

[Export("scrollViewDidScroll:")]
public void Scrolled(UIScrollView scrollView){
  nfloat delta = scrollView.ContentOffset.Y;
  nfloat newPosition = CurrentLocationOfFab.Y + delta;

  var location = new CGPoint(ControllerFabContainer.Frame.Location.X, newPosition);
  ControllerFabContainer.Frame = new CGRect(location, ControllerFabContainer.Frame.Size);
}

如果像我的案例一样使用BottomTabNavigator,则需要在新位置添加+80,以覆盖mainView中Tab键大小的错误。

希望这对以后的所有人都有帮助。

0 个答案:

没有答案
相关问题