Wednesday, September 25, 2019

How to create a custom header in tableview section that stretch when orientation changes

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == TableSections.OtherDevices.rawValue {
            let v = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: SetupConstants.DEFAULT_HEADER_HEIGHT))

            self.spinner.center = CGPoint(x:(tableView.bounds.size.width - (SetupConstants.DEFAULT_HEADER_HEIGHT / 2)), y:SetupConstants.DEFAULT_HEADER_HEIGHT/2)
            spinner.hidesWhenStopped = true
            spinner.startAnimating()
            spinner.color = UIColor.blue
            
            v.addSubview(spinner)
            spinner.autoresizingMask = .flexibleLeftMargin
            v.autoresizingMask = .flexibleWidth
            return v
        }
        return nil
    }