2016-07-14 16 views
4

我的意思是浮動視圖是自定義視圖,它是滾動視圖的子視圖(或似乎是子視圖),滾動直到它錨定在某個點上。類似的行爲將是UITableView的節頭。下面如何製作浮動視圖?

floating view image

我的內容視圖(在浮動視圖下方的視圖)附加的圖像是不是在的tableview佈局。這意味着如果我僅將tableview用於浮動視圖,則必須將我的內容視圖放置在1個鉅細胞內,或將其分解爲具有不同佈局的多個細胞。內容視圖將有很多動態元素,這就是爲什麼我不想將它放入UITableViewCell中,除非必須。我可以編程浮動視圖/在scrollview上使用自動佈局嗎?

+0

這是一個主要基於觀點的**問題,請報告關於您問題的更多詳細信息。 –

+0

我以前也見過它,我的經驗是使用自動佈局似乎不太好,有時它有一些延遲動畫,而我使用KVO,它會好得多。順便說一句,使用表格視圖更容易,正如你所說的 – childrenOurFuture

+0

你說的_「我不需要頁面的tableview」_,但從技術上說,你已經排除了使用浮動部分標題的明顯解決方案,並且沒有顯示任何努力爲了提供任何其他解決方案,您只需在這裏放下一個要求,以便通過__someone__ else和__somehow__來解決您的問題 - 但這不是如何在此提出問題的方式。 – holex

回答

0

使用KVO更新浮動視圖的幀。

這裏是用Objective-C的代碼示例:

// ScrollView.m 
// ScrollView is a subclass of UIScrollView 

@interface ScrollView() 

@property (nonatomic, strong) UIView *floatingView; 
@property (nonatomic) CGRect originalBorderFrame; 
@property (nonatomic) CGFloat anchorHeight; 

@end 

@implementation ScrollView 


- (instancetype)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 

    if (self) { 
     self.floatingView = [UIView new]; 
     self.floatingView.backgroundColor = [UIColor colorWithRed:0.8211 green:0.5 blue:0.5 alpha:1.0]; 
     self.floatingView.frame = CGRectMake(0, 150, frame.size.width, 20); 
     self.originalBorderFrame = self.floatingView.frame; 
     [self addSubview:self.floatingView]; 

     self.anchorHeight = 44; 

     [self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil]; 
    } 

    return self; 
} 

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

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context { 
    if ([keyPath isEqualToString:@"contentOffset"]) { 
     if (self.contentOffset.y > self.originalBorderFrame.origin.y-self.anchorHeight) { 
      self.floatingView.frame = CGRectOffset(self.originalBorderFrame, 0, self.contentOffset.y - (self.originalBorderFrame.origin.y-self.anchorHeight)); 
     } 
    } 
} 
@end 

這裏是捕捉:

enter image description here

2

使用的tableview節頭可能是最好的解決方案,可以隨時輕鬆定製單元或單元本身的數量以實現特定的佈局。

但是,如果你絕對不想處理tableview,這個組件看起來非常酷,它實際上是要添加到tableview,但我用twitter示例測試它,你可以將它添加到一個滾動視圖,所以你不需要一個表視圖,它會工作,給道具的人。 GSKStretchyHeaderView

希望這會有所幫助,評論如果您有任何問題,祝您好運。