ios - Xamarin 表单 - IOS : How to detect the UIView size changed
<p><p>我使用 ContentView 创建了一个 Xamarin 表单,并为 Android 创建了一个渲染器。现在我必须为 IOS 创建一个渲染器。 </p>
<p>在 android 渲染器中,我可以覆盖 onSizeChanged 并将这些宽度/高度值传递给自定义 xamarin-formView 。 </p>
<pre><code>protected override void OnSizeChanged(int w, int h, int oldw, int oldh) {
base.OnSizeChanged(w,h, oldw, old);
Element.SizChanged(w,h);
</code></pre>
<p>}</p>
<p>IOS中的UIView是否有类似的覆盖方法?我已经尝试覆盖 Frame 属性并在 Element.SizeChanged(w,h) 中调用,但我会得到 TargetInvocationException。 </p>
<p>附: SizeChanged 方法是我的自定义虚拟方法而不是 android 方法。 </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>覆盖 <code>LayoutSubviews</code> 通常用于<em>跟踪调整大小事件</em>,因为 <code>UIView</code> 也负责它的所有 subview 。 </p>
<p>我也倾向于覆盖 <code>Bounds</code> 和 <code>Frame</code> 取决于我在做什么以及这个 View 包含什么......取决于父级, View 的 <code>LayoutSubviews </code> 可以调用很多...</p>
<p>假设 UIView 子类如下:</p>
<pre><code>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();
}
}
</code></pre>
<p>锻炼它:</p>
<pre><code>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);
</code></pre>
<p>结果:</p>
<pre><code>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>>
</code></pre>
<p>有关更多信息,请参阅此 SO:<a href="https://stackoverflow.com/questions/4000664/is-there-a-uiview-resize-event" rel="noreferrer noopener nofollow">Is there a UIView resize event?</a> </p></p>
<p style="font-size: 20px;">关于ios - Xamarin 表单 - IOS : How to detect the UIView size changed,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39461968/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39461968/
</a>
</p>
页:
[1]