Add clickable and fixed subview to UITableViewController?

瘦欲@ 提交于 2019-12-05 02:54:24

问题


I'd like to place an ADBannerView object onto my UITableView screen statically, what means that I want it to always stay above my toolbar (self.navigationController.toolbar), even when the user is scrolling the tableview. I've solved this by adding by ADBannerView as a subview to my toolbar and given it negative values for the frames origin:

[self setBannerViewSize];
[self.navigationController.toolbar addSubview:bannerView];

The only problem is: I can't click and open the iAd this way - I can see the banner but nothing happens when I tap on it.

Since I'm also using a refreshControl, the option to use a UIViewController instead of UITableViewController and add a tableView manually wouldn't work for me. Is there any other way I can get my ADBannerView statically showing in my table view controller AND still being tappable?

Thank you in advice!


回答1:


Yay!! After all I succeeded in solving this (really annoying) problem by myself (and a lot of reading around)!

First, I found this really world-changing post. Basically this post handles with the topic that a UITableViewController uses self.view for its tableView property, so overriding the tableView property (or synthesizing it manually) plus giving self.view a new view (from application) and adding tableView as its subview would make it possible to reach the real superview of tableView.

But this still didn't solve my problem, although I was sure it would, because it all made sense. My bannerView appeared in the right place (and was fixed) but it still didn't do anything when clicked. But there was a second minor thing I didn't know about:

As I read in this post the superview of a subview doesn't only have to be userInteractionEnabled but also have a non-transparent backgroundColor. Because my superviews background color was set to [UIColor clearColor] it all didn't work - but setting its backGroundColor to e.g. blackColor solved the whole problem: the bannerView got finally tappable! :)

So, my code is now looking like this:

@synthesize tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (!tableView && [self.view isKindOfClass:[UITableView class]]) {
        tableView = (UITableView *)self.view;
    }

    self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    self.tableView.frame = self.view.bounds;
    [self.view addSubview:self.tableView];

    [self resizeTableToFitBanner];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bannerView];

    // some other code
}



回答2:


BannerViewController in Apple's iAdSuite sample code solves this problem very elegantly:

https://developer.apple.com/library/ios/samplecode/iAdSuite/Introduction/Intro.html




回答3:


I think you should use a container view, and set things up in IB. You can add a tool bar and ADBannerView to the bottom of the view of your navigation controller's root view controller. Fill the rest of the space with a container view - this will give you an embedded view controller automatically. You should delete this one and then drag in a tableViewController and control drag from the container view to the tableViewController to hook up the embed segue.



来源:https://stackoverflow.com/questions/13634707/add-clickable-and-fixed-subview-to-uitableviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!