Resizing UITableView When Displaying AdWhirl Ads A

2019-08-02 02:01发布

问题:

I am trying to integrate AdWhirl into my iPhone app using an AppDelegate singleton so I can use the same AdWhirl instance across multiple views, but cannot figure out how to resize the tables in those views. The code I am using is:

in ...AppDelegate.h:

#import "AdWhirlView.h"
#import "AdWhirlDelegateProtocol.h"

@interface ...AppDelegate : NSObject <UIApplicationDelegate, AdWhirlDelegate>
AdWhirlView *awView;

...

@property (nonatomic, retain) AdWhirlView *awView;

in ...AppDelegate.m didFinishLaunchingWithOptions:

awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];

also in ...AppDelegate.m I add the required delegate methods

(NSString *)adWhirlApplicationKey...
(UIViewController *)viewControllerForPresentingModalView...

This code allows me to display the same Ad across multiple views but I cannot figure out how to resize a UITableView to change its height to be shorter if an Ad is displaying, so that the UITableView either displays full height if there is no Ad, or is resized if there is an Ad at the bottom of the screen. I have the UITableView as a subview of a UIViewController called myMainView.

I tried changing the autosize properties in the Nib file for the UITableView to have a variable spacer at the bottom, and am adding the AdWhirl instance into the view with this code:

...AppDelegate * myDelegate = (...AppDelegate *)[[UIApplication sharedApplication] delegate];
[myDelegate.awView setFrame:CGRectMake(0, 480-20-44-50, 320, 50)];
[self.myMainView addSubview: myDelegate.awView];

This displays the Ad at the correct location at the bottom of the screen but the UITableView is not resizing. How should I be doing this?

回答1:

I think you have to create a UIView with an embedded UITableView. I've tried to do something similar and this was the only way I could get it to work. A top-level UITableView is auto-resized to take up the entire screen.

Just expanding on that, you probably want to declare something in your header like so:

@interface ExampleClass:UIViewController {
    UITableView *tableView;
}

@property (nonatomic,retain) IBOutlet UITableView *tableView;

Then in your actual implementation, you can resize that declared tablview whenever you need to by doing:

CGRect tableFrame = tableView.frame;
//Decrease the height of table by height of ad banner
tableFrame.size.height = tableView.frame.size.height - adBannerView.frame.size.height;

tableView.frame = tableFrame;