2011-11-04 93 views
3

我创建了一个视图控制器,并在XCode 4.2(界面生成器)中将视图的方向设置为横向。但是,添加视图时,视图以纵向显示。我已经覆盖所有视图控制器中的ShouldAutorotateToInterfaceOrientation以返回true,并且我试图使用以下代码手动旋转视图:单击 - 在横向上创建的视图以纵向显示

this.View.Transform.Rotate(3.14f * .5f);

我也尝试将帧设置为横向帧(即480 X 320),尽管视图的帧已经正确设置。

只是为了澄清,我的绝大多数意见是在肖像。我希望以横向方向加载横向视图,而不管设备实际位于何种方向。这是可能的吗?如果是这样,我错过了什么?

在此事先感谢您的帮助!

更新:我注意到ShouldAutorotateToInterfaceOrientation仅在视图加载时调用一次。设备旋转时不会调用它。这是正常的行为吗?

+0

当你说你重写了ShouldAutorotateToInterfaceOrientation返回true ...你的意思是你正在返回以下?:return toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight; – Anuj

回答

-1

我认为对于总是希望在横向方向中显示的横向视图,您需要在ShouldAutorotateToInterfaceOrientation内返回False或完全移除覆盖。这将防止设备处于纵向模式时自动旋转视图。

+0

我已经尝试了ShouldAutorotateToInterfaceOrientation的每个不同选项(返回true,返回false,全部删除),并且行为没有改变。这个问题并不妨碍自动旋转。该视图根本不旋转。它只是在肖像中显示。XCode 4.2(界面生成器)中的视图的“模拟度量标准”下的“方向”选项是否指定视图应显示在哪个方向,还是我需要做其他操作才能使方向旋转? – Auc

+0

模拟指标中的设置都不会在运行时影响用户界面,它们只会显示“假设”。这是一个想法:你有没有在你的Mono项目设置中指出你支持所有的设备方向?打开您的项目设置,展开Build,然后进入iPhone应用程序区域。在那里,你会发现你的应用程序支持的方向列表。确保它们全部被选中,然后恢复ShouldAutorotateToInterfaceOrientation覆盖并返回true。 –

+0

感谢competent_tech,但仍然没有变化。我认为指定一个方向将是微不足道的,但显然,我错了。我是否也应该通过this.View.Transform.Rotate旋转视图(3.14f * 0.5f)?尽管这条线似乎不影响任何方式。 – Auc

4

每次设备方向更改时都会调用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,一旦这个视图弹出堆栈,方向将恢复正常。

相关问题