How do I add an extra separator to the top of a UITableView?

后端 未结 8 2124
面向向阳花
面向向阳花 2021-02-06 22:20

I have a view for the iPhone that is basically split in two, with an informational display in the top half, and a UITableView for selecting actions in the bottom half. The prob

相关标签:
8条回答
  • 2021-02-06 22:49

    To replicate the standard iOS separator lines, I use a 1 px (not 1 pt) hair line tableHeaderView with the table view's separatorColor:

    // in -viewDidLoad
    self.tableView.tableHeaderView = ({
        UIView *line = [[UIView alloc] 
                        initWithFrame:CGRectMake(0, 0,
                        self.tableView.frame.size.width, 1 / UIScreen.mainScreen.scale)];
        line.backgroundColor = self.tableView.separatorColor;
        line;
    });
    

    The same in Swift (thanks, Dane Jordan, Yuichi Kato, Tony Merritt):

    let px = 1 / UIScreen.main.scale
    let frame = CGRect(x: 0, y: 0, width: self.tableView.frame.size.width, height: px)
    let line = UIView(frame: frame)
    self.tableView.tableHeaderView = line
    line.backgroundColor = self.tableView.separatorColor
    
    0 讨论(0)
  • 2021-02-06 22:53

    I made a UITableView extension that displays a native style separator on top of the UITableView, while the table gets scrolled.

    Here is how it looks

    Here's the code (Swift 3)

    fileprivate var _topSeparatorTag = 5432 // choose unused tag
    
    extension UITableView {
    
        fileprivate var _topSeparator: UIView? {
            return superview?.subviews.filter { $0.tag == _topSeparatorTag }.first
        }
    
        override open var contentOffset: CGPoint {
            didSet {
                guard let topSeparator = _topSeparator else { return }
    
                let shouldDisplaySeparator = contentOffset.y > 0
    
                if shouldDisplaySeparator && topSeparator.alpha == 0 {
                    UIView.animate(withDuration: 0.15, animations: {
                        topSeparator.alpha = 1
                    })
                } else if !shouldDisplaySeparator && topSeparator.alpha == 1 {
                    UIView.animate(withDuration: 0.25, animations: {
                        topSeparator.alpha = 0
                    })
                }
            }
        }
    
        // Adds a separator to the superview at the top of the table
        // This needs the separator insets to be set on the tableView, not the cell itself
        func showTopSeparatorWhenScrolled(_ enabled: Bool) {
            if enabled {
                if _topSeparator == nil {
                    let topSeparator = UIView()
                    topSeparator.backgroundColor = separatorColor?.withAlpha(newAlpha: 0.85) // because while scrolling, the other separators seem lighter
                    topSeparator.translatesAutoresizingMaskIntoConstraints = false
    
                    superview?.addSubview(topSeparator)
    
                    topSeparator.leftAnchor.constraint(equalTo: self.leftAnchor, constant: separatorInset.left).isActive = true
                    topSeparator.rightAnchor.constraint(equalTo: self.rightAnchor, constant: separatorInset.right).isActive = true
                    topSeparator.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
                    let onePixelInPoints = CGFloat(1) / UIScreen.main.scale 
                    topSeparator.heightAnchor.constraint(equalToConstant: onePixelInPoints).isActive = true
    
                    topSeparator.tag = _topSeparatorTag
                    topSeparator.alpha = 0
    
                    superview?.setNeedsLayout()
                }
            } else {
                _topSeparator?.removeFromSuperview()
            }
        }
    
        func removeSeparatorsOfEmptyCells() {
            tableFooterView = UIView(frame: .zero)
        }
    }
    

    To enable it, simply call tableView.showTopSeparatorWhenScrolled(true) after you set your delegate for your UITableView

    0 讨论(0)
提交回复
热议问题