我已创建BaseViewController以实现可重用性。有顶。 BaseViewController中的横幅图像。我正在尝试从孩子那里修改它,但是它不起作用。
BaseVC:
import UIKit
import SnapKit
class BaseViewController: UIViewController {
public let bannerImageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.addSubview(bannerImageView)
bannerImageView.snp.makeConstraints { (make) in
make.left.right.top.equalTo(self.view)
make.height.equalTo(200)
}
self.view.backgroundColor = .white
self.navigationController?.navigationBar.isHidden = true
}
}
HomeViewController:
import UIKit
class HomeViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
self.bannerImageView.image = UIImage(named: "HomeTopBanner")
}
}
[当您在子类中重写方法时,应像这样调用super方法。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.bannerImageView.image = UIImage(named: "HomeTopBanner")
}