I try to center a view which is displayed in my iPad App depending on interface orientation.
That doesn’t work, the view is always shifted depending on in which orientation the app started.
I’ve searched through Stackoverflow for 2 hours now, tried different methods but couldn’t make it work.
This is how i display the view from root view:
publicationDownload = [[PublicationDownloadController alloc] init];
// Setting up propertys to load Publication-related information
[publicationDownload setDelegate:self];
[publicationDownload setThePubID:[NSNumber numberWithInt:[sender tag]]];
publicationDownload.view.opaque = NO;
publicationDownload.view.backgroundColor = [UIColor clearColor];
[publicationDownload.theUIView.layer setCornerRadius:5];
[publicationDownload.theUIView.layer setShadowColor:[UIColor blackColor].CGColor];
[publicationDownload.theUIView.layer setShadowOpacity:0.5];
[publicationDownload.theUIView.layer setShadowRadius:5];
[publicationDownload.theUIView.layer setShadowOffset:CGSizeMake(3.0f, 3.0f)];
publicationDownload.theUIView.clipsToBounds = NO;
publicationDownload.theUIView.backgroundColor = [UIColor clearColor];
[publicationDownload.theInnerUIView.layer setCornerRadius:5];
[publicationDownload.theInnerUIView.layer setShadowColor:[UIColor blackColor].CGColor];
[publicationDownload.theInnerUIView.layer setShadowOpacity:0.5];
[publicationDownload.theInnerUIView.layer setShadowRadius:5];
[publicationDownload.theInnerUIView.layer setShadowOffset:CGSizeMake(3.0f, 3.0f)];
[publicationDownload.theInnerUIView.layer setMasksToBounds:YES];
publicationDownload.theUIView.layer.masksToBounds = NO;
publicationDownload.view.clipsToBounds = YES;
publicationDownload.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5];
[self.view addSubview:publicationDownload.view];
within the publicationDownloadViewController i’ve implemented:
in viewDidLoad:
// Do any additional setup after loading the view from its nib.
//self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//self.view.contentMode = UIViewContentModeRedraw;
self.view.autoresizesSubviews = YES;
//self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view.frame = self.view.bounds;
//theUIView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
NSLog(@"PublicationDownloadController: viewDidLoad:");
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
theUIView.frame = CGRectMake(212, 229, 600, 240);
self.view.frame = CGRectMake(0, 0, 1024, 768);
} else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
theUIView.frame = CGRectMake(140, 380, 600, 240);
self.view.frame = CGRectMake(0, 0, 768, 1024);
}
and additional:
-(void)didRotate:(NSNotification*)notification
{
NSLog(@"PublicationDownloadController: didRotate");
UIDeviceOrientation oreo = [[UIDevice currentDevice] orientation];
// oreo is our new orientation!
if (oreo == UIInterfaceOrientationPortrait || oreo == UIInterfaceOrientationPortraitUpsideDown) {
//theUIView.frame = CGRectMake(212, 229, 600, 240);
self.view.frame = self.view.bounds;
} else if (oreo == UIInterfaceOrientationLandscapeLeft || oreo == UIInterfaceOrientationLandscapeRight) {
//theUIView.frame = CGRectMake(140, 380, 600, 240);
self.view.frame = self.view.bounds;
}
}
the View itself contains a view in landscape, which background is set to black and alpha 0.5 (in IB)-
Additional there is another View (theUIView) which holds some UIControls. This theUIView needs to be centered. It works if ste app starts in Landscape for the landscape view, but not if the orientation changes to portrait. Also if the app starts in portrait, the view is not displayed centered.
Any help is much appreciated!
It was easier than expected. Dealing with autoresizemask in Interface Builder and cleaning some messy code did the job.