Search code examples
swiftuiviewviewdidload

Using viewdidload to show some element in the UIView


By using an online course, I made a calendar via writing codes without using storyboard. The problem is, In the page that I want to add this calendar, there is a popup page which is UIView, for example, it is

@IBOutlet weak var PopUpCalenderView: UIView!

in that course, the calendar just adds in in the view controller by adding few lines of codes in the viewdidload.

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "My Calender"
    self.navigationController?.navigationBar.isTranslucent=false
    self.view.backgroundColor=Style.bgColor

    view.addSubview(calenderView)
    calenderView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10).isActive=true
    calenderView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -12).isActive=true
    calenderView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 12).isActive=true
    calenderView.heightAnchor.constraint(equalToConstant: 365).isActive=true

    let rightBarBtn = UIBarButtonItem(title: "Light", style: .plain, target: self, action: #selector(rightBarBtnAction))
    self.navigationItem.rightBarButtonItem = rightBarBtn
}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    calenderView.myCollectionView.collectionViewLayout.invalidateLayout()
}
}

When I use this method, it will add the calendar behind that the popup page (UIView) and it is in the background. Could you help me to how I can solve this problem and move the calendar to the popup page? Thank you so much


Solution

  • Add your calendar as a subview to your popupview instead of the view controller view.

    popupView.addSubview(calendarView)