Monotouch - 以横向创建的视图以纵向显示 [英] Monotouch - View created in landscape orientation is displayed in portrait orientation

查看:87
本文介绍了Monotouch - 以横向创建的视图以纵向显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a view controller and set the orientation of the view to Landscape in XCode 4.2 (Interface Builder). However, when I add the view, the view is displayed in Portrait orientation. I've overriden ShouldAutorotateToInterfaceOrientation in all view controllers to return true, and I've attempted to rotate the view manually using the following code:

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

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

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

I have also tried to set the frame to a landscape frame (i.e. 480 X 320), though the frame of the view is already set correctly.

为了澄清,我的绝大多数观点都是在Portrait中。我想以横向方向加载横向视图,而不管设备实际的方向。这可能吗?如果是这样,我错过了什么?

Just to clarify, the vast majority of my views are in Portrait. I would like to load the landscape view in a landscape orientation, irrespective of what orientation the device is actually in. Is this possible? If so, what am I missing?

提前感谢您对此事的任何帮助!

Thanks in advance for any assistance on this matter!

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

UPDATE: I'm noticing that ShouldAutorotateToInterfaceOrientation is only called once when the view is loaded. It is not called when the device is rotated. Is this normal behavior?

推荐答案

每次设备方向改变时都会调用ShouldAutorotateToInterfaceOrientation。

ShouldAutorotateToInterfaceOrientation is called every time the device orientation changes.

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模式下定位,并且每次都将新的设备方向写入控制台。

this code will only allow the view to orient itself in either landscapeleft or landscaperight mode, and writes the new device orientation to the console every time.

当你加载视图,并将其推到(例如)UINavigationController但是,它仍然是纵向的模式(并且不调用ShouldAutorotateToInterfaceOrientation。)

when you load the view, and push it onto (for instance) a UINavigationController however, it's still in portrait mode (and ShouldAutorotateToInterfaceOrientation is not called.)

从Objective-C开始,强制设备方向相当容易,但在MonoTouch中,似乎没有直接映射。

From Objective-C, it's fairly easy to force a device orientation, but in MonoTouch, there appears to be no direct mapping.

解决方案;

向objective-c运行时发送消息,指定我们想要的方向。

Send a message to the objective-c runtime, specifying which orientation we want.

您可以通过将以下内容添加到视图控制器来轻松完成此操作:

you can easily do this by adding the following to a view controller:

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,一旦这个视图从堆栈中弹出,方向将恢复正常。

you can now manually change the orientation of the entire device. What's more, is that if you use a UINavigationController, the orientation will return back to normal once this view is popped off the stack.

这篇关于Monotouch - 以横向创建的视图以纵向显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆