aherlambang
aherlambang

Reputation: 14418

adding UIButton to window rotation issues

I am doing:

UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    CGPoint point = CGPointMake(window.frame.size.width/2, window.frame.size.height/2);
    point = CGPointMake(window.frame.size.width - v.frame.size.width - 10, 30);
    point = CGPointMake(point.x + offsetLeft, point.y + offsetTop);
    v.center = point;
    timer1 forMode:NSDefaultRunLoopMode];
    [window addSubview:v];

One issue is that when I do in landscape mode:

UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];

and I print:

NSLog(@"WINDOW WIDTH IS %f AND HEIGHT IS %f", window.frame.size.width, window.frame.size.height);

I got:

WINDOW WIDTH IS 768.000000 AND HEIGHT IS 1024.000000

Which is not true.. it seems that it doesn't care about orientation.

Upvotes: 0

Views: 691

Answers (4)

Brandon Schlenker
Brandon Schlenker

Reputation: 5088

I think only the first subview of your window actually receives rotation events. Try this.

[[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] addSubview:SOME_SUBVIEW];

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299455

You should not insert views into the UIWindow. The UIWindow itself does not rotate. It applies transforms to a rotation subview, which holds the root view. You should be inserting your view into the root view if at all possible. Otherwise, you will need to calculate your own transforms, and that's a pain.

See this question for more details on this issue. The short answer is that this kind of thing is best done by using the View-Based Application template, and making use of the rootViewController to get the root view and insert there.

Note that you should also avoid asking for the first window of the UIApplication. This is not guaranteed to be the "main" window; the first window is the furthest-back window (and it is not unusual for there to be more than one window). If you must use a UIWindow, get it from a view you care about ([view window]), or bind it to your UIApplicationDelegate in the XIB.

Upvotes: 5

Nick Weaver
Nick Weaver

Reputation: 47241

You can find out which orientation your device is facing with

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

Then you could perform a check on it:

if (orientation == UIDeviceOrientationPortrait) {
    // ...
} else {
    // ...
}

Upvotes: 0

sridevi
sridevi

Reputation: 627

Use [[UIScreen mainScreen] bounds] to get the frame. It will return the appropriate frame information in all orientations. I faced the similar problem. And I dont know the reason for it.

Upvotes: 0

Related Questions