强制MKAnnotationView Callout Bubble在内容更改时调整大小

时间:2016-02-29 20:48:50

标签: c# ios xamarin.ios xamarin.forms

给出以下代码段

public sealed class BindableMapAnnotationView : MKAnnotationView
{
    private readonly BindableMapCallout _calloutView;
    private readonly BindableMapCalloutContainer _view;

    public BindableMapAnnotationView(BindableMapAnnotation xfxAnnotation, string className, BindableMapCallout calloutView)
        : base(xfxAnnotation, className)
    {
        _calloutView = calloutView;
        _view = new BindableMapCalloutContainer(_calloutView);
        Layer.AnchorPoint = new CGPoint(0.5f, 1.0f);
        Image = xfxAnnotation.Marker;
        CanShowCallout = true;
        Selected = true;
        AutosizesSubviews = true;
        DetailCalloutAccessoryView = _view;

        _calloutView.MeasureInvalidated += CalloutViewOnMeasureInvalidated;
    }

    private void CalloutViewOnMeasureInvalidated(object sender, EventArgs eventArgs)
    {
        // how to resize the bubble around the DetailCalloutAccessoryView?
    }

    public void OnAppearing()
    {
        _calloutView.OnAppearing();
    }

    public void OnDisappearing()
    {
        _calloutView.OnDisappearing();
    }

    protected override void Dispose(bool disposing)
    {
        _calloutView.MeasureInvalidated -= CalloutViewOnMeasureInvalidated;
        base.Dispose(disposing);
    }
}

每当_calloutView更改内容时,它也会重新绘制视图,并且我可以在屏幕上看到新内容。问题在于白色"泡沫"当内容发生变化时,UIView周围没有调整大小。我尝试(无济于事)使这个方法适用于此。

private void CalloutViewOnMeasureInvalidated(object sender, EventArgs eventArgs)
{
    _viewContainer.SizeThatFits(new CGSize());
    _viewContainer.LayoutSubviews();
    var size = _viewContainer.Bounds;
    var rect = new CGRect(0, 0, size.Width, size.Height);
    DetailCalloutAccessoryView.Bounds = rect;
    DetailCalloutAccessoryView.SetNeedsDisplay();
    // bubble still does NOT change it's dimensions.
}


FYI, the container looks like this.


internal sealed class BindableMapCalloutContainer : UIView
{
    private readonly IVisualElementRenderer _renderer;

    public BindableMapCalloutContainer(BindableMapCallout calloutView)
    {
        _renderer = Platform.CreateRenderer(calloutView);
        Platform.SetRenderer(calloutView, _renderer);
        AddSubview(_renderer.NativeView);
    }

    // if writing another container in iOS, prefer to NOT use this unless you have to (Jason Smith)
    public override CGSize IntrinsicContentSize
    {
        get { return SizeThatFits(new CGSize()); }
    }

    public override CGSize SizeThatFits(CGSize size)
    {
        return _renderer.Element.GetSizeRequest(double.PositiveInfinity, double.PositiveInfinity).Request.ToSizeF();
    }

    public override void LayoutSubviews()
    {
        _renderer.Element.Layout(Bounds.ToRectangle());
    }
}

在下面的屏幕截图中,地址字段最初设置为Loading...,一旦我们反向地理定位地址,标签的大小就会改变,从而导致重新绘制。视图按预期重新绘制,但白色气泡没有。

有任何想法的人吗?

callout 1

callout 2

callout 3

0 个答案:

没有答案