24

So, as the title says, I have an hdmi out on the iPad and an observer registered for screen connections, upon connection the user chooses the res and a view is outputted.

However, if I load a view from a nib, or even from a programatic view controller, the ipad shows a landscape view in portrait (yes, both situations are set to landscape).

I.e.

ExternalViewController *ex = [[ExternalViewController alloc] init];
[externalWindow setRootViewController:ex];

does this:

Bad If I create the view itself programatically. like so:

UIView *test = [[UIView alloc] initWithFrame:[externalScreen applicationFrame]];
[test setBackgroundColor:[UIColor whiteColor]];
UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 100, 30)];
msgLabel.text = @"External!";
[test addSubview:msgLabel];

It runs like some form of magical dream:

good

However I want the viewcontroller to load (and work!) so, StackOverflow, I ask you. has anyone come across this before?

EDIT: It does go without saying that common sensical answers do not get a bounty, I am after a fix, not a workaround. With my limited brain, all I can think to do is create a method that creates a view based on it's inputs and adds that as a subview of the external monitor, it is clear that this is a hack solution so a fix is appreciated! Thanks!

EDIT:

-(id)initWithFrame:(CGRect)_rect 
{
    rect = _rect;
    if (self = [super init]) 
    {
        externalView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"105.png"]];
        externalView.alpha = 0.0;
        [externalView setFrame:rect];
        [externalView setBackgroundColor:[UIColor yellowColor]];
        self.view = [[UIView alloc] initWithFrame:rect];
        self.view.backgroundColor = [UIColor whiteColor];

        return self;
    }
}

- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:rect];
    self.view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:externalView];
}

As requested, this is how I am loading the viewcontroller, initialising with the size of the external screen. Thanks

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
Christopher Gwilliams
  • 1,321
  • 15
  • 29
  • Is the "landscape view in portrait" truncated, or rotated? I mean, if you add a label as in your second example, does this appear in the top left of the screen, or the bottom left at a 90 degree angle? – jrturton Nov 14 '11 at 16:19
  • Sorry, should have explained. When you see the view in half of the screen it is landscape but rotated. So, if I add a label then it shows it rotated with the text going from bottom to top. – Christopher Gwilliams Nov 14 '11 at 16:26
  • Could you include code from the `loadView` method of your 'nibless' view controller, showing how you are creating the views, where it gets the dimensions from for the external display, etc? – jrturton Nov 14 '11 at 16:31
  • Observation: In the top example, it looks like your adding the External View Controller to the UIWindow, but the second one looks like it's being added to the UIScreen? – TigerCoding Nov 17 '11 at 21:08
  • Nope, in the case of the view controller, it is being set as the root view. In the case of the view, it is just added as a subview. – Christopher Gwilliams Nov 17 '11 at 22:09
  • use `self.tableView` instead of `[UIScreen mainScreen]` – Synxmax Nov 21 '11 at 14:03

1 Answers1

4

Just return NO in - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation in your UIViewController subclass.

// ugly, don't use this in real code

if ([UIScreen screens].count == 1) return; // just one screen, eww.

// getting the secondary screen
UIScreen *screen = [[UIScreen screens] objectAtIndex:1];

__block UIScreenMode *highestWidthMode = NULL;

[screen.availableModes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  UIScreenMode *currentModeInLoop = obj;
  if (!highestWidthMode || currentModeInLoop.size.width > highestWidthMode.size.width)
    highestWidthMode = currentModeInLoop;
}];

// setting to the highest resolution available
screen.currentMode = highestWidthMode;

NSLog(@"screen.currentMode = %@", screen.currentMode);
screen.overscanCompensation = UIScreenOverscanCompensationScale;


// initializing screen
secondWindow = [[UIWindow alloc] initWithFrame:[screen bounds]];
[secondWindow setScreen:screen];

// other view is a UIViewController, just remember to return NO in - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation or the iOS will rotate the frame.

UIViewController *vc = [[OtherView alloc] initWithNibName:@"OtherView" bundle:nil];
secondWindow.rootViewController = vc;
[secondWindow makeKeyAndVisible];
Marcelo Alves
  • 1,856
  • 11
  • 12