简体   繁体   English

自定义UIViewController对设备旋转没有响应

[英]Custom UIViewController is not responsive to device rotation

I have a custom UIViewController, which is the only subView of UIView. 我有一个自定义UIViewController,这是UIView的唯一子视图。 The UIViewController contains delegate function: UIViewController包含委托函数:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

This function is called once when the application starts and is never called again when the device is rotated. 当应用程序启动时,将调用此函数一次,而旋转设备时,将不再调用此函数。 I also notice that the willRotateToInterfaceOrientation function is never called. 我还注意到,从未调用willRotateToInterfaceOrientation函数。 I pretty much commented out all the content in the UIViewController but it is still not responding to device rotation. 我几乎注释掉了UIViewController中的所有内容,但它仍未响应设备旋转。

I ended up solving my own problem by starting from stretch to create a brand new UIViewController and made sure it was responsive to the device rotation. 我从一开始就创建了一个全新的UIViewController并确保它对设备旋转做出了响应,从而解决了自己的问题。 I then brought in my code piece by piece and checked the rotation. 然后,我将代码逐段引入并检查了旋转情况。 In the end, I found the root cause. 最后,我找到了根本原因。 In my custom UIViewController, I had 在我的自定义UIViewController中,

- (id)initWithFrame:(CGRect)theframe {
    if (self.view = [super.view initWithFrame:theframe])

It worked find excpet it did not respond to device roation even though I did not call the init function. 即使我没有调用init函数,它也可以找到excpet,它不会对设备旋转做出响应。 The solution is simple. 解决方案很简单。 Add [self init] in the initWithFrame function. 在initWithFrame函数中添加[self init]。 Thank you all for responding. 谢谢大家的回应。

Your code looks correct. 您的代码看起来正确。 I suspect it's something in your .xib file (like the wrong object type for "File's Owner"), so that perhaps your view controller subclass isn't being instantiated at all. 我怀疑这是您的.xib文件中的内容(例如“文件所有者”的对象类型错误),因此可能根本没有实例化您的视图控制器子类。 Put some logging into viewDidLoad and make sure it's getting called. 将一些日志记录放入viewDidLoad ,并确保它被调用。

Try doing a test app that just tests the rotation problem. 尝试做一个仅测试旋转问题的测试应用。 This will help isolate your issue. 这将有助于隔离您的问题。

您可能需要在父UIView的控制器中实现该方法,因为您的视图似乎包含在另一个视图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM