當嘗試根據設備方向在單個視圖控制器中動態分配約束時,我遇到意外的行爲。帶VFL約束的iOS動態自動撥號
期望的行爲:如下面的代碼所示,我使用VFL將視圖附加到超級視圖的右側,頂部和左側邊緣,縱向模式時的高度爲300,然後切換到具有在橫向模式下,左側和頂部邊緣的高度爲90,寬度爲160。
if (orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown)
{
View.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[green(300)]", 0, metrics, views));
View.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[green]|", 0, new NSDictionary(), views));
}
else
{
View.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[green(90)]", 0, metrics, views));
View.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[green(160)]", 0, metrics, views));
}
經驗的行爲:我們也看不到在視圖上的初始負荷爲肖像模式所需的行爲也時轉向橫向模式。但是,當返回到肖像模式時,每當此設備轉到臨時模式但橫向模式繼續運行時,視圖控制將消失。簡而言之,肖像模式僅適用於第一次。
我已經做了相當廣泛的研究,無法確定問題。我會非常感謝對解決方案的任何洞察力。
下面是完整的代碼,以防萬一:
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System;
using System.CodeDom.Compiler;
namespace iOS.UI
{
partial class TestConstraintsController : UIViewController
{
public TestConstraintsController (IntPtr handle) : base (handle)
{
}
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
AdjustGeometry();
}
public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
{
base.DidRotate (fromInterfaceOrientation);
AdjustGeometry();
}
public void AdjustGeometry()
{
UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
NSDictionary views = NSDictionary.FromObjectsAndKeys (
new NSObject[] { greenBox },
new NSObject[] { new NSString ("green") }
);
View.RemoveConstraints (View.Constraints);
if (orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown)
{
View.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[green(boxHeightPortrait)]", 0, new NSDictionary(), views));
View.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[green]|", 0, new NSDictionary(), views));
}
else
{
View.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[green(90)]", 0, new NSDictionary(), views));
View.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[green(160)]", 0, new NSDictionary(), views));
}
}
}
}
非常感謝! – Razor