2013-04-29 59 views
0

我想添加一個視圖到集合視圖和表視圖(因此適用於任何類型的滾動視圖)的內容視圖的底部,我也希望能夠向下滾動以查看此視圖​​,例如:UICollectionView等計算內容大小,以便我可以永久增加它?

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     // Observe change in content size so can move my view when 
     // content size changes (keep it at the bottom) 
     [self addObserver:self forKeyPath:@"contentSize" 
        options:(NSKeyValueObservingOptionPrior) 
        context:nil]; 
     CGRect frame = CGRectMake(0, 0, 200, 30); 
     self.loadingView = [[UIView alloc] initWithFrame:frame]; 
     [self.loadingView setBackgroundColor:[UIColor blackColor]]; 
     [self addSubview:self.loadingView]; 
     // Increase height of content view so that can scroll to my view. 
     self.contentSize = CGSizeMake(self.contentSize.width, self.contentSize.height+30); 

    } 
    return self; 
} 

然而,當,例如,被插入細胞中的contentSize被重新計算並同時我的觀點是在內容大小的底部仍然可見(由於是超生滾動視圖)我不能再滾動到它。

如何確保內容大小保持不變,正如在我的代碼中那樣,高30點?

另外一個問題是:

  • 有沒有其他的方法來跟蹤觀察比它的其他內容的大小?

在此先感謝。


我曾嘗試:

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 
    self.contentSize = CGSizeMake(self.contentSize.width, self.contentSize.height+30); 
} 

然而這會導致各種各樣的顯示問題。

+0

你可能想在layoutSubviews做這些計算。 – atreat 2013-04-29 18:59:25

+0

這是UICollectionView等通常計算contentSize的地方嗎? – Michael 2013-04-29 19:00:23

+0

我沒有看到代碼中的任何內容會改變您正確的「contentSize' – matt 2013-04-29 19:05:28

回答

0

如果我理解正確,你想要在底部的tableView(f.e.)中顯示一個加載視圖。你可以添加一個額外的包含這個LoaderView的UITableViewCell到tableView。 (必須更改numberOfRowsInTableView)

對於scrollViews的另一個角度:使用較小的邊界,然後使用內容本身,以使其可滾動。例如frame =全屏。在子視圖中添加或修改每個單元格(添加)contentSize =內容大小+ 30 px。

+0

是的,這是我目前所擁有的,但是我的應用程序中有5個實例, UITableView和UICollectionViews加載視圖。我試圖通過將其構建到UIxxxView本身中以某種方式進行抽象。 – Michael 2013-04-29 19:11:22

+0

可能通過永久增加單元格數量並設置最後一個單元格的大小來在ViewController中對其進行抽象,但是我喜歡使用委託協議在觸發「觸發器行」時向接收器發送消息的想法,然後委託插入更多數據。 – Michael 2013-04-29 19:14:48

+0

在這種情況下:如何將類LoaderView作爲屬性添加到UIScrollView類以及覆蓋的setContentSize方法,該方法使用loaderView的高度自動增加contentSize.y。你可以在這裏找到更多有關房產的信息:http:// oleb。淨/博客/ 2011/05 /僞造,高德式-objc類別與 - 聯想引用/ – Peteee24 2013-04-29 19:24:38

0

嘗試製作滾動視圖的子類並重寫contentSize getter,以便始終返回30 px。

- (CGSize)contentSize { 
    CGSize customContentSize = super.contentSize; 
    customContentSize.height += 30; 
    return customContentSize; 
} 

(我寫的內存代碼,可能有誤差)

相關問題