中心视图不起作用

Posted

技术标签:

【中文标题】中心视图不起作用【英文标题】:center view doesn't work 【发布时间】:2011-12-21 15:35:18 【问题描述】:

我尝试根据界面方向将显示在我的 iPad 应用程序中的视图居中。 这不起作用,视图总是会根据应用启动的方向发生变化。

我已经通过 *** 搜索了 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 方法? 我这样做了,但是这两种方法都没有被调用。 【参考方案1】:

这比预期的要容易。在 Interface Builder 中处理 autoresizemask 并清理一些杂乱的代码就可以了。

【讨论】:

以上是关于中心视图不起作用的主要内容,如果未能解决你的问题,请参考以下文章

在中心缩放实时 iPhone 相机视图,“CGAffineTransformTranslate”不起作用

UICollectionView:ScrollToItem 到中心单元格在水平集合视图上不起作用

添加约束滚动后不起作用

UIButton 在 iOS 5.x 中不起作用,在 iOS 6.x 中一切正常

TextView 重力属性不起作用

Swift - 设置 CALayer 边界或框架不起作用