每次设备方向更改时都会调用ShouldAutorotateToInterfaceOrientation。
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
Console.Out.WriteLine(toInterfaceOrientation.ToString());
// Return true for supported orientations
// This particular screen is landscape only!
return (toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight);
}
此代码将只允许以使自身在任一landscapeleft或landscaperight模式,以及新设备的方向每次写入控制台。
时加载的观点,并将其推入(例如)一个UINavigationController然而,它仍处于纵向模式(而ShouldAutorotateToInterfaceOrientation不叫。)
从Objective-C的
,这也很容易给力设备方向,但在MonoTouch中,似乎没有直接映射。
该解决方案;
发送消息到objective-c运行时,指定我们想要的方向。
可以很容易地通过加入以下视图控制器执行以下操作:
Selector orientationSetter;
private void SetOrientation (UIInterfaceOrientation toOrientation)
{
if (orientationSetter == null)
{
orientationSetter = new Selector ("setOrientation:");
}
Messaging.void_objc_msgSend_int (UIDevice.CurrentDevice.Handle,
orientationSetter.Handle, (int)toOrientation);
}
现在可以手动改变整个装置的取向。更重要的是,如果你使用UINavigationController,一旦这个视图弹出堆栈,方向将恢复正常。
当你说你重写了ShouldAutorotateToInterfaceOrientation返回true ...你的意思是你正在返回以下?:return toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight; – Anuj