Xamarin表单-IOS:如何检测已更改的UIView大小 [英] Xamarin Form - IOS: How to detect the UIView size changed

查看:142
本文介绍了Xamarin表单-IOS:如何检测已更改的UIView大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ContentView创建了Xamarin Form,并为Android创建了渲染器.现在,我必须为IOS创建一个渲染器.

I created a Xamarin Form using ContentView and created a renderer for Android. Now I have to create a renderer for IOS.

在android渲染器中,我可以覆盖onSizeChanged并将那些宽度/高度值传递给自定义xamarin-form视图.

In android renderer, I can override onSizeChanged and passing those width/height value to the custom xamarin-form view.

protected override void OnSizeChanged(int w, int h, int oldw, int oldh) { 
        base.OnSizeChanged(w,h, oldw, old);

        Element.SizChanged(w,h);

}

IOS中的UIView是否具有类似的方法来覆盖?我尝试覆盖Frame属性并在Element.SizeChanged(w,h)中调用,但是我将获得TargetInvocationException.

Does UIView in IOS has the similar method to override? I have tried override the Frame property and invoke in Element.SizeChanged(w,h), but I will get TargetInvocationException.

P.S. SizeChanged方法是我的自定义虚拟方法,而不是android方法.

P.S. SizeChanged method is my custom virtual method not android method.

推荐答案

覆盖LayoutSubviews通常用于跟踪调整大小的事件,因为UIView还要负责其所有子视图.

Overriding LayoutSubviews is normally used to track resize events as a UIView is responsible to for all of it's subviews also.

根据我在做什么以及该视图包含的内容,我也倾向于覆盖BoundsFrame ...根据父级,该视图的LayoutSubviews可以称为很多...

I tend to also override Bounds and Frame depending upon what I am doing and what this view contains... Depending upon the parent, the View's LayoutSubviews can be called a lot...

假设一个UIView子类如下:

Assuming a UIView subclass like:

public class UIViewResizing : UIView
{
    public UIViewResizing() { }
    public UIViewResizing(CGRect frame) : base(frame) { }
    public UIViewResizing(NSCoder coder) : base(coder) { }

    public override CGRect Bounds
    {
        get
        {
            return base.Bounds;
        }
        set
        {
            if (value != base.Bounds)
            {
                Console.WriteLine($"Bounds changing: {this}");
            }
            base.Bounds = value;
        }
    }

    public override CGRect Frame
    {
        get
        {
            return base.Frame;
        }
        set
        {
            if (value != base.Frame)
            {
                Console.WriteLine($"Frame changing: {this}.");
            }
            base.Frame = value;
        }
    }

    public override void LayoutSubviews()
    {
        Console.WriteLine($"LayoutSubViews requested: {this}");
        base.LayoutSubviews();
    }
}

行使:

var view = new UIViewResizing(new CGRect(100, 100, 100, 100));
view.BackgroundColor = UIColor.Red;
Add(view);
await Task.Delay(2500);
view.Frame = new CGRect(100, 100, 200, 200);
await Task.Delay(2500);
view.Bounds = new CGRect(20, 20, 100, 200);

结果:

Frame changing: <uiviewresize_UIViewResizing: 0x79a6cc00; frame = (-50 -50; 100 100); layer = <CALayer: 0x78628230>>.
LayoutSubViews requested: <uiviewresize_UIViewResizing: 0x79a6cc00; frame = (100 100; 100 100); layer = <CALayer: 0x78628230>>
Frame changing: <uiviewresize_UIViewResizing: 0x79a6cc00; frame = (100 100; 100 100); layer = <CALayer: 0x78628230>>. 
LayoutSubViews requested: <uiviewresize_UIViewResizing: 0x79a6cc00; frame = (100 100; 200 200); layer = <CALayer: 0x78628230>>
Bounds changing: <uiviewresize_UIViewResizing: 0x79a6cc00; frame = (100 100; 200 200); layer = <CALayer: 0x78628230>>
LayoutSubViews requested: <uiviewresize_UIViewResizing: 0x79a6cc00; frame = (150 100; 100 200); layer = <CALayer: 0x78628230>>

请咨询该SO以获取更多信息:是否有UIView调整大小事件?

Consult this SO for additional information: Is there a UIView resize event?

这篇关于Xamarin表单-IOS:如何检测已更改的UIView大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆