MDCFlexibleHeaderView height issue
MDCFlexibleHeaderView height issue
I'm trying to use the MDCFlexibleHeader, in a ViewController with a UIScrollView. The headerView is always set to the minimumHeight, which is weird because I set a maximumHeight also. I think it's because of the way I set my constraints, but I'm not sure I'm doing wrong with my constraints. I tried to use the following as example code
https://github.com/material-components/material-components-ios/tree/develop/demos/Shrine
Here is my code:
import UIKit
import MaterialComponents.MaterialFlexibleHeader
class MainFlexibleHeaderContainerViewController: MDCFlexibleHeaderContainerViewController {
init() {
let viewController = MainViewController(nibName: nil, bundle: nil)
super.init(contentViewController: viewController)
viewController.headerViewController = self.headerViewController
viewController.setupHeaderView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
import UIKit
import MaterialComponents.MaterialFlexibleHeader
class MainViewController: UIViewController {
var headerViewController: MDCFlexibleHeaderViewController!
var scrollView: UIScrollView!
var contentView: UIView!
var myHeaderView = MyHeaderView(frame: .zero)
var heightConstraint: NSLayoutConstraint!
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nil, bundle: nil)
setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewWillAppear(_ animated: Bool) {
setupNavigationBar()
}
private func setupNavigationBar() {
self.navigationController?.setNavigationBarHidden(true, animated: false)
UIApplication.shared.statusBarStyle = .lightContent
}
private func setupView() {
scrollView = UIScrollView()
scrollView.contentInset = UIEdgeInsets(top: -20, left: 0, bottom: 0, right: 0)
scrollView.bounces = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.delegate = headerViewController
view.addSubview(scrollView)
view.addConstraintsWithFormat(format: "H:|[v0]|", views: scrollView)
view.addConstraintsWithFormat(format: "V:|[v0]|", views: scrollView)
contentView = UIView()
contentView.backgroundColor = .white
scrollView.addSubview(contentView)
scrollView.addConstraintsWithFormat(format: "H:|[v0]|", views: contentView)
scrollView.addConstraintsWithFormat(format: "V:|[v0]|", views: contentView)
view.addConstraint(NSLayoutConstraint(item: contentView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1, constant: 0))
heightConstraint = NSLayoutConstraint(item: contentView, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, constant: 0)
heightConstraint.priority = .defaultLow
view.addConstraint(heightConstraint)
let view1 = UIView()
view1.backgroundColor = .blue
contentView.addSubview(view1)
contentView.addConstraintWithFormat(format: "H:|[v0]|", views: view1)
contentView.addConstraintWithFormat(format: "V:|[v0]|", views: view1)
}
func setupHeaderView() {
let headerView = headerViewController.headerView
headerView.trackingScrollView = scrollView
headerView.maximumHeight = 400
headerView.minimumHeight = 100
headerView.minMaxHeightIncludesSafeArea = true
headerView.backgroundColor = UIColor.white
headerView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
myHeaderView.backgroundColor = .red
myHeaderView = (headerView.bounds)
myHeaderView = [.flexibleWidth, .flexibleHeight]
headerView.addSubview(myHeaderView)
}
}
This is what it looks:
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.