开发者

Custom UIViewController is not responsive to device rotation

开发者 https://www.devze.com 2022-12-23 03:35 出处:网络
I have a custom UIViewController, which is the only subView of UIView. The UIViewController contains de开发者_高级运维legate function:

I have a custom UIViewController, which is the only subView of UIView. The UIViewController contains de开发者_高级运维legate function:

- (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. I pretty much commented out all the content in the UIViewController but it is still not responding to device rotation.


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. 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

- (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. The solution is simple. Add [self init] in the initWithFrame function. 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. Put some logging into viewDidLoad and make sure it's getting called.


Try doing a test app that just tests the rotation problem. This will help isolate your issue.


You may need to implement that method in the controller of the parent UIView, as it seems your view is enclosed in another view.

0

精彩评论

暂无评论...
验证码 换一张
取 消