In my application, depending on whether the user is logged in, header looks differently. The problem is that by calling it in viewDidLoad – it loads incorrectly.
Here’s my header code:
func configureUITableViewHeader() {
let header = HomeTableHeaderView.fromNib()
header.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 200)
header.backgroundColor = AppColors.mainThemeColor.withAlphaComponent(0.3)
let bottomLine = UIView(frame: CGRect(x:0, y: header.frame.height, width:header.frame.width , height:3))
bottomLine.backgroundColor = AppColors.detailsColor
header.addSubview(bottomLine)
switch UserAccount.shared.state {
case .verified:
header.configure(delegate: self, labeltext: "Добро пожаловать! (String(describing: UserAccount.shared.userEmail!))")
header.logOutButtonUotlet.isHidden = false
header.logInButtonOutlet.isHidden = true
tableView.tableHeaderView = header
case .nonVerified:
header.configure(delegate: self, labeltext: "Пожалуйста, авторизуйтесь чтобы продолжить")
header.logInButtonOutlet.isHidden = false
header.logOutButtonUotlet.isHidden = true
tableView.tableHeaderView = header
default :
print("nothing to showing")
}
}
If i call it in ViewDidAppear:
What could be the problem? I thought I was familiar enough with the controller lifecycle, but …
2
Answers
viewDidLoad is called when the view is loaded into the memory.
Note: The view is not completely rendered at this point of time hence, you will not get the exact size of the parent view or the window.
viewDidAppear is called when the view is added to the view hierarchy. Here, the view rendering is complete. This is where you will get the correct size of it’s parent view or the window.
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621423-viewdidappear
to layout the header properly I use the following code in my viewController