简体   繁体   English

Xamarin.iOS-自定义UIPopoverBackgroundView

[英]Xamarin.iOS - custom UIPopoverBackgroundView

I need to implement a custom popover for iOS 6+ using Xamarin.iOS. 我需要使用Xamarin.iOS为iOS 6+实现自定义弹出窗口。

So far, everything is working except turning off the default shadow. 到目前为止,除了关闭默认阴影之外,其他所有操作都有效。 Here is my implementation: 这是我的实现:

    public class PopoverBackgroundView : UIPopoverBackgroundView
    {
        private UIImageView _arrow, _background;

        public override float ArrowOffset { get; set; }

        public override UIPopoverArrowDirection ArrowDirection { get; set; }

        [Export ("arrowHeight")]
        static new float GetArrowHeight()
        {
            return 39;
        }

        [Export ("arrowBase")]
        static new float GetArrowBase()
        {
            return 80;
        }

        [Export ("contentViewInsets")]
        static new UIEdgeInsets GetContentViewInsets()
        {
            return new UIEdgeInsets (25, 25, 25, 25);
        }

        [Export("wantsDefaultContentAppearance")]
        static new bool WantsDefaultContentAppearance
        {
            get { return false; }
        }

        public PopoverBackgroundView(IntPtr handle) : base (handle)
        {
            _background = new UIImageView { Image = Theme.PopoverBackgroundImage };
            _arrow = new UIImageView { Image = Theme.PopoverArrowImage };

            AddSubview(_background);
            AddSubview(_arrow);
        }

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

            var arrowFrame = new RectangleF(Frame.Width - GetArrowBase(), -14, GetArrowBase(), GetArrowHeight());
            _arrow.Frame = arrowFrame;

            var backgroundFrame = Frame;
            backgroundFrame.X = 
                backgroundFrame.Y = 0;
            _background.Frame = backgroundFrame;
        }
    }

This class is pretty weird, because you have to "override" static Objective-C values. 该类很奇怪,因为您必须“覆盖”静态Objective-C值。 The only way to do this in C# is declaring a static new member and adding the Export attribute. 在C#中执行此操作的唯一方法是声明一个static new成员并添加Export属性。

Everything is working except for WantsDefaultContentAppearance , which is never called. 除了WantsDefaultContentAppearance之外, WantsDefaultContentAppearance所有东西都在工作,它从未被调用。 According to Apple documentation here , it will disable the default shadow on the popover. 根据此处的 Apple文档,它将禁用弹出框上的默认阴影。 Has anyone gotten this to work with Xamarin.iOS? 有人让它可以与Xamarin.iOS一起使用吗? I think it could possibly be a Xamarin bug since all the methods work fine, and this one happens to be a property. 我认为这可能是Xamarin的错误,因为所有方法都能正常工作,而这恰好是一个属性。 The methods in this class seem to be defined as methods "on purpose" in this class, since they would look a lot prettier as properties. 此类中的方法似乎被定义为此类中的“有目的”方法,因为它们在属性上看起来更漂亮。

I don't think this is a Xamarin bug, but the iOS APIs were just not working for me. 我认为这不是Xamarin错误,但iOS API不适用于我。

To fix this, my designer took his shadow off the images and we just kept the default shadow. 为了解决这个问题,我的设计师将阴影从图像上去除了,我们只保留了默认阴影。

Honestly, I think this is better anyway, because Apple took the time to make a pretty shadow--why remove it? 老实说,我认为这还是更好的选择,因为苹果花了一些时间来画一个漂亮的影子-为什么要删除它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM