2011-12-21 106 views
0

我尝试根据界面方向将显示在我的iPad应用程序中的视图居中。 这不起作用,视图始终移位,取决于应用程序的启动方向。中央视图不起作用

我已经通过Stackoverflow搜索了2个小时,尝试了不同的方法,但无法使其工作。

我这是怎么显示从根视图的观点:publicationDownloadViewController内

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]; 

我实现:

viewDidLoad: //做任何额外的设置加载从视图后的笔尖。

//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); 
} 

和附加:

-(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; 
} 
} 

视图本身包含在景观的视图,其背景设置为黑色和α0.5(在IB) - 附加有另一个视图(theUIView),其拥有一些UIControls。这个theUIView需要居中。它适用于如果横向视图中的横向应用程序启动了Ste应用程序,但如果方向更改为纵向,则不适用。此外,如果应用程序以纵向显示,视图不会显示居中。

任何帮助非常感谢!

+0

为什么不重写' - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation'方法的UIViewController类? – 2011-12-21 19:08:09

+0

我这样做了,但两种方法都没有被调用。 – MadMaxAPP 2011-12-22 12:28:19

回答

0

这比预期的更容易。在Interface Builder中处理autoresizemask并清理一些杂乱的代码完成了这项工作。