中心视图不起作用

时间:2011-12-21 15:35:19

标签: ios view uiview orientation

我尝试将我的iPad App中显示的视图居中,具体取决于界面方向。 这不起作用,视图总是根据应用程序的开始方向移动。

我现在已经在Stackoverflow中搜索了2个小时,尝试了不同的方法,但无法使其正常工作。

这是我从根视图显示视图的方式:

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

在publicationDownloadViewController中,我实现了:

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

视图本身包含横向视图,背景设置为黑色和alpha 0.5(在IB中) - 另外还有另一个View(theUIView),它包含一些UIControl。这个theUIView需要集中。如果应用程序在横向视图中以横向视图启动,则可以正常工作,但如果方向更改为纵向,则无效。此外,如果应用程序以纵向方式启动,则视图不会居中显示。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

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