LittleFunny
LittleFunny

Reputation: 8405

Xamarin Form - IOS: How to detect the UIView size changed

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

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);

}

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 method is my custom virtual method not android method.

Upvotes: 2

Views: 2220

Answers (1)

SushiHangover
SushiHangover

Reputation: 74209

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

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...

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();
    }
}

Exercising it with:

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);

Results in:

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>>

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

Upvotes: 3

Related Questions