iOS:将UIView置于固定位置的UITableView之上

时间:2011-09-24 08:17:49

标签: ios uitableview uiview

我需要在我的iphone应用程序中的UITableView上放置一个UIView(用于广告)。问题是,当我将表格滚动到底部时,添加的UIView将与表格一起滚动。我想要的是将它固定在屏幕的底部。有没有办法做到这一点?

这是我用来将UIView添加到表中的代码:

 awView = [AdWhirlView requestAdWhirlViewWithDelegate:self]; 
 awView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
 [self.tableView addSubview:awView];

6 个答案:

答案 0 :(得分:33)

以下是它对我有用的方式。广告停留在视图的底部。

在ViewDidLoad中,在YourController.m中:

awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
awView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height-kAdWhirlViewHeight/2);
[self.view addSubview:awView];

然后在同一个.m文件中的某处添加此方法:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect newFrame = awView.frame;
    newFrame.origin.x = 0;
    newFrame.origin.y = self.tableView.contentOffset.y+(self.tableView.frame.size.height-kAdWhirlViewHeight);
    awView.frame = newFrame;
}

不要忘记声明awView。

答案 1 :(得分:29)

我很欣赏这是一个老问题。但我发现答案的部分内容都是虚假信息,而且片段不清楚。因此,对于它仍然值得的东西,我在这里是如何在UITableViewController视图的底部添加“浮动”视图。是的,你可以这样做,即使接受的答案说你做不到。

-viewDidLoad方法中,您可以创建一个我们将其命名为bottomFloatingView的视图。这也是一个属性。

请务必在表格视图的底部添加内容插件,这样可以避免使用浮动视图隐藏任何表格的内容。

接下来,您应该使用UIScrollViewDelegate更新浮动视图的框架。

幻想是你的观点被困在底部。实际上,此视图在您滚动时始终在移动,并且始终被计算为显示在底部。滚动视图非常强大!可能是我认为最被低估的UIKit课程之一。

所以这是我的代码。请注意该属性,表视图上的内容插入以及-scrollViewDidScroll:委托方法实现。我在故事板中创建了我的浮动视图,这就是为什么你看不到正在设置的原因。

另外不要忘记你应该也可以使用KVO来观察表视图框架的变化。有可能随着时间的推移而改变,最简单的测试方法是在模拟器中打开和关闭呼叫状态栏。

最后,如果您在表格视图中使用节标题视图,那么这些视图将是表格视图中的最顶层视图,因此您还需要将浮动视图放在前面,当您执行此操作时改变它的框架。

@interface MyTableViewController ()
@property (strong, nonatomic) IBOutlet UIView *bottomFloatingView;
@end

@implementation MyTableViewController

static NSString *const cellIdentifier = @"MyTableViewCell";

- (void)dealloc
{
    [self.tableView removeObserver:self forKeyPath:@"frame"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView addSubview:self.bottomFloatingView];

    self.tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, CGRectGetHeight(self.bottomFloatingView.bounds), 0.0);
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, CGRectGetHeight(self.bottomFloatingView.bounds), 0.0);

    [self.tableView addObserver:self
                     forKeyPath:@"frame"
                        options:0
                        context:NULL];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];

    return cell;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self adjustFloatingViewFrame];
}

#pragma mark - KVO

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    if([keyPath isEqualToString:@"frame"]) {
        [self adjustFloatingViewFrame];
    }
}

- (void)adjustFloatingViewFrame
{
    CGRect newFrame = self.bottomFloatingView.frame;

    newFrame.origin.x = 0;
    newFrame.origin.y = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.bounds) - CGRectGetHeight(self.bottomFloatingView.bounds);

    self.bottomFloatingView.frame = newFrame;
    [self.tableView bringSubviewToFront:self.bottomFloatingView];
}

@end

答案 2 :(得分:10)

  1. 将您的视图添加到表格视图的超级视图中(如果可能; UITableViewController使这不可能)。

  2. 将您的视图添加到表格视图中,并在-scrollViewDidScroll:委托方法中重新定位(UITableViewDelegateUIScrollViewDelegate的子协议)。

答案 3 :(得分:4)

我有一个类似的问题,我想在我的UITableViewController上添加一个加载指示器。为了解决这个问题,我将我的UIView添加为窗口的子视图。这解决了这个问题。这就是我做到的。

-(void)viewDidLoad{
[super viewDidLoad];
//get the app delegate
XYAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

//define the position of the rect based on the screen bounds
CGRect loadingViewRect = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 50, 50);
//create the custom view. The custom view is a property of the VIewController
self.loadingView = [[XYLoadingView alloc] initWithFrame:loadingViewRect];
//use the delegate's window object to add the custom view on top of the view controller
[delegate.window addSubview: loadingView]; 
}

答案 4 :(得分:1)

对于像我这样正在寻找使用 Swift 的简单解决方案的人来说,这些答案有点过时了。这是我所做的(假设 myCustomView 是在文件中的其他地方建立的):

array1 = [1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10]
array2 = [15,16,17,18,19]
# Stack two arrays to make a single np.array
LST = np.hstack([array1, array2])
print(LST.shape)
>>> (24,)

# Now you can calculate mean, percentile etc. without 0's
np.mean(LST[LST != 0])
>>> 5.5
np.percentile(LST[LST != 0], [5, 85])
>>> array([1.45, 8.65])

答案 5 :(得分:0)

- (void)viewDidLoad
{
    [super viewDidLoad];

    footerView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-64, SCREEN_WIDTH, 64)];
    footerView.backgroundColor = [UIColor blueColor ];
    [self.navigationController.view addSubview:footerView];
}   

- (void)dealloc
{
    [footerView removeFromSuperview];
}