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

后端 未结 8 2121
面向向阳花
面向向阳花 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
    

提交回复
热议问题