• 0
Votes
name
name Punditsdkoslkdosdkoskdo

Swift 4 - Xcode 9.4.1 - Header width doesn't fit screen in ios

UITableView Header Width Problem

I have a UITableView with sections and headers. I have set the header's background color in storyboard, but when I run it on simulator or real device, the header width does not fit the screen.

The problem occurs when I select a device with smaller screen size from View as in storyboard devices and run the app on a device with larger screen.

Storyboard Device Preview

Header view contains only a label and its constraints are fit to superview with no spaces. The point is that the label is centered correctly! but the background color does not reach the end of view.

I have already tried to change the header width using frame.size.width in the following methods:

  • viewForHeaderInSection

  • awakeFromNib()

  • viewDidLayoutSubviews()

  • didEndDisplayingHeaderView

Thanks in advance.

Solved:

The problem was another view in header which was not constrained properly.

Thanks for sparing time and effort to find the problem ;;;