KISS

Keep It Simple Stupid

Third-party UIViewController's orientation fix

| comments

What to do if you have a framework that provides a modal view controller for iPad to do some actions, but it only allows to be presented in portrait orientation, and your app supports landscape orientation only? It looked very weird when the user uses the app in landscape mode, clicks a button, and the view swipes from left, rotated 90 degrees clockwise. You have no source code to change that. In my case, there was no reason to limit the supported orientations of the framework’s controller, as it could easily fit in landscape-oriented screen.

The first thought that popped up in my head was to apply transformations to rotate the view. It’s a very hacky solution though, thus it can be used as a last resort.

Luckily, due to the dynamic nature of Objective-C, I managed to rotate the view with the help of the category:

FrameworkController+OrientationFix.h
1
2
3
4
5
6
7
/* The category fixes FrameworkController's not supporting
 landscape orientation out of box.
 It also hides the application's status bar.
 Works on iPad only. */
@interface FrameworkController (OrientationFix)

@end
FrameworkController+OrientationFix.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import "FrameworkController+OrientationFix.h"

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

@implementation FrameworkController (OrientationFix)

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  
  if (IS_IPAD) {
      [UIApplication sharedApplication].statusBarHidden = YES;
  }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  if (IS_IPAD) {
      return YES;
  } else {
      return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
  }
}

@end

And no need to create a child class. Instead just include the header with the category and you’re set.

A few notes here. First, I also hid the status bar (as the app doesn’t show it). Second, these amendments are applied on iPads only.

dev, iOS

Note: The comments in the blog are provided by disqus.com; if you don't see the comment form under the post, probably your browser or its extension (such as uBlock Origin or NoScript) blocks their scripts.

« LinkedListT in Bada HashMapT in Bada »

Comments