Search code examples
iosswiftuikittableviewtableheader

contentInsetAdjustmentBehavior is never set


I'm trying to set the contentInsetAdjustmentBehavior == .never for the UITableView in the viewDidLoad() of the ViewController, however it is ignored, no matter if I do it programmatically or via the InterfaceBuilder.

Here is what I get. The image is the header of the TableView.

Here is what I get. The image is the header of the TableView.

I have the navigation bar on top of that which is transparent by the way.

I expect the Header with the image to be like this

expected behavior

Any help's appreciated. Thanks


Solution

  • enter image description hereSet the topConstraint constant of imageView to -64

    • create an Outlet for topConstraint of imageView
    • declare a variable topPadding (for iPhone X)

      @IBOutlet weak var topConstraint: NSLayoutConstraint!
      var topPadding:CGFloat = 0.0
      

    And use the below code in viewDidload

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController!.navigationBar.shadowImage = UIImage()
        self.navigationController!.navigationBar.isTranslucent = true
        topConstraint.constant = -64
        if #available(iOS 11.0, *) {
            let window = UIApplication.shared.keyWindow
            topPadding = (window?.safeAreaInsets.top)!
            topConstraint.constant = -(64+topPadding)
        }
    }
    

    Refer this sample project