【发布时间】:2011-12-21 15:35:18
【问题描述】:
我尝试根据界面方向将显示在我的 iPad 应用程序中的视图居中。 这不起作用,视图总是会根据应用启动的方向发生变化。
我已经通过 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:
// 在从 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);
}
以及其他:
-(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 中)-
另外还有另一个视图 (theUIView) 包含一些 UIControls。这个theUIView 需要居中。如果 ste 应用程序在横向视图中启动,它可以工作,但如果方向更改为纵向则不能。此外,如果应用以纵向启动,则视图不会居中显示。
非常感谢任何帮助!
【问题讨论】:
-
为什么不重写 UIViewController 类的
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation方法? -
我这样做了,但是这两种方法都没有被调用。
标签: ios view uiview orientation