OStack程序员社区-中国程序员成长平台

标题: ios - Xamarin 表单 - IOS : How to detect the UIView size changed [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 22:02
标题: ios - Xamarin 表单 - IOS : How to detect the UIView size changed

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

在 android 渲染器中,我可以覆盖 onSizeChanged 并将这些宽度/高度值传递给自定义 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。

附: SizeChanged 方法是我的自定义虚拟方法而不是 android 方法。



Best Answer-推荐答案


覆盖 LayoutSubviews 通常用于跟踪调整大小事件,因为 UIView 也负责它的所有 subview 。

我也倾向于覆盖 BoundsFrame 取决于我在做什么以及这个 View 包含什么......取决于父级, View 的 LayoutSubviews 可以调用很多...

假设 UIView 子类如下:

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:Is there a UIView resize event?

关于ios - Xamarin 表单 - IOS : How to detect the UIView size changed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39461968/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4