天天看點

iOS UITableView 指定組頭懸停位置

懸停位置

當​

​UITableView​

​​占滿螢幕時

組頭預設是在​​

​頂部​

​​懸停

在機型 ​​

​iPhone X​

​​ 上

會被頂部的​​

​劉海​

​給擋住

這效果顯然不能直視~

理想的效果是:

在​​

​導航​

​下方懸停

(導航是個自定義的視圖

帶有漸變效果)

contentInset

官方文檔:

​​

​The custom distance that the content view is inset from the safe area or scroll view edges.​

參考代碼:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat tableHeaderViewHeight = CGRectGetHeight(self.tableView.tableHeaderView.bounds);
    // 內插補點 = 頭視圖高度 - 導覽列高度 
    if (offsetY >= tableHeaderViewHeight - TopBarHeight) {
        // 頂部偏移距離:導覽列高度
        self.tableView.contentInset = UIEdgeInsetsMake(TopBarHeight/*用你自己定義的宏*/, 0, 0, 0);
    } else {
        self.tableView.contentInset = UIEdgeInsetsZero;
    }
}      

參考

有趣的驗證碼輸入框

繼續閱讀