尽管其框架设置为y = 0,但我的视图显示y = -20。旋转后,它会快速回到y = 0

时间:2010-06-13 15:31:04

标签: iphone objective-c ipad

我开始创建一个基于通用窗口的应用程序。从iPhone版本开始,我创建了一个UIViewController和相关的nib。

我的应用代表:

rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[window makeKeyAndVisible];
[window addSubview:rootViewController.view];
return YES;

我的RootViewController:

- (void)viewDidLoad {
[super viewDidLoad];
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()];
[self.view addSubview:adBannerView];

}

我尝试过设置按钮而不是adBanner,我得到的结果相同。

我的RootViewController的nib没有被更改,因为x-code为我创建了它。 我的MainWindow_iPhone.xib也有库存。

造成这种情况的原因是什么?

更新

更改应用程序的方向后,adBannerView(或按钮...)将在y = 0处捕捉到正确的位置。我已经尝试将adBannerView的y位置设置为20,可能是为了补偿状态栏,这使得所有内容都能正确显示,直到我改变方向。然后一切都向下移动20个像素,并在adBannerView和状态栏之间留出20个像素的空间。

3 个答案:

答案 0 :(得分:6)

尝试添加viewDidLoad中的下一行([super viewDidLoad];之后):

self.view.frame = [[UIScreen mainScreen] applicationFrame];

答案 1 :(得分:0)

CGRectZero实际上是一个零rect(0,0,0,0),所以ADBannerView应该永远不会出现,如果它的宽度和高度确实为0.你可能想尝试{ {1}}左右......

答案 2 :(得分:0)

您应该在添加视图之前设置尺寸标识符:

- (void)viewDidLoad {
[super viewDidLoad];
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()];

if(UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
    adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
else
    adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;


[self.view addSubview:adBannerView];

// now you can treat it like any other subview
// For example, if you want to move it to the bottom of the view, do this:

CGRect frame = adBannerView.frame;
frame.origin.y = self.view.frame.size.height - frame.size.height;
[adBannerView setFrame:frame];

}

每当界面旋转时,您应该通知横幅更改其大小。

假设您可以访问WWDC视频(可免费使用),请检查视频会话305.它演示添加横幅。