My view is displaying y=-20 dispite its frame set

2020-02-13 03:22发布

I started by creating a universal window based app. Starting with the iPhone version I created a UIViewController and associated nib.

My App delegate:

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

My RootViewController:

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

}

I've tried instanciating buttons instead of the adBanner and I get the same result.

My RootViewController's nib has not been changed since x-code created it for me. My MainWindow_iPhone.xib also is stock.

What's causing this?

Update

After changing the app's orientation the adBannerView (or button...) will snap into the correct place at y=0. I've tried setting adBannerView's y location to 20 presumably to compensate for the status bar and that makes everything display correctly until I change orientation. Then everything moves down 20 pixels and will leave a 20 pixel space between the adBannerView and the status bar.

3条回答
beautiful°
2楼-- · 2020-02-13 03:43

CGRectZero is literally a zero rect (0, 0, 0, 0), so ADBannerView should never show up if it really has a width and height of 0. You probably want to try initWithFrame:self.view.frame or so…

查看更多
趁早两清
3楼-- · 2020-02-13 04:05

Try to add the next line in your viewDidLoad (right after [super viewDidLoad];):

self.view.frame = [[UIScreen mainScreen] applicationFrame];
查看更多
我想做一个坏孩纸
4楼-- · 2020-02-13 04:07

You should set the size identifier before adding the view:

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

}

Whenever the interface rotates, you should notify the banner to change its size.

Assuming you have access to WWDC videos (which is available for free), check video session 305. It demos adding the banner.

查看更多
登录 后发表回答