簡體   English   中英

iOS:將 UIView 放在固定位置的 UITableView 之上

[英]iOS: Place UIView on top of UITableView in fixed position

我需要在我的 iphone 應用程序中的 UITableView 頂部放置一個 UIView(用於廣告)。 問題是,當我將表格滾動到底部時,添加的 UIView 與表格一起滾動。 我想要的是將它固定在屏幕底部。 有沒有辦法做到這一點?

這是我用來將 UIView 添加到表的代碼:

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

這是它對我的工作方式。 廣告停留在視圖的底部。

在 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。

我很欣賞這是一個老問題。 但我已經找到了部分錯誤信息和不清楚片段的答案。 所以對於它仍然值得,這里是我如何在UITableViewController的視圖底部添加一個“浮動”視圖。 是的,您可以這樣做,即使公認的答案說您不能。

在您的-viewDidLoad方法中,您可以創建一個視圖,我們將其命名為 bottomFloatingView。 這也設置為屬性。

確保在表格視圖的底部添加內容插入,這將避免使用浮動視圖隱藏表格的任何內容。

接下來,您應該使用UIScrollViewDelegate來更新浮動視圖的框架。

錯覺是你的觀點被卡在了底部。 實際上,此視圖在您滾動時一直在移動,並且始終被計算為顯示在底部。 滾動視圖非常強大! 並且可能是我認為最被低估的 UIKit 類之一。

所以這是我的代碼。 注意屬性,表格視圖上的內容插入和-scrollViewDidScroll:委托方法實現。 我在我的故事板中創建了我的浮動視圖,這就是為什么你看不到它正在設置的原因。

另外不要忘記你可能還應該使用 KVO 來觀察 table view 框架的變化。 它可能會隨着時間的推移而改變,最簡單的測試方法是打開和關閉模擬器中的通話狀態欄。

最后一件事,如果您在表格視圖中使用部分標題視圖,這些視圖將是表格視圖中最頂部的視圖,因此您還希望將浮動視圖置於最前面,在更改其框架時執行此操作.

@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
  1. 將您的視圖添加到表視圖的超級視圖(如果可能; UITableViewController使這成為不可能)。

  2. 將您的視圖添加到表視圖並在-scrollViewDidScroll:委托方法( UITableViewDelegateUIScrollViewDelegate的子協議)中重新定位它。

我有一個類似的問題,我想在 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]; 
}

對於像我這樣正在尋找使用 Swift 的簡單解決方案的人來說,這些答案有點過時了。 這是我所做的(假設 myCustomView 是在文件中的其他地方建立的):

func scrollViewDidScroll(_ scrollView: UIScrollView) {
   
    let pixelsFromBottom = CGFloat(20)//or whatever the
    let theHeight = self.tableView.frame.height + scrollView.contentOffset.y
    myCustomView.frame = CGRect(x: 0, y: theHeight - pixelsFromBottom , width: self.view.frame.width, height: myCustomView.frame.height)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM