Search code examples
swiftbuttondelegatesxib

How to hide/show Xib view on button click in swift


I have created xib view(AdvertisementView) programatically and here i have added Delegate method for button

import UIKit
protocol SampleButtonViewDelegate: AnyObject {
func sampleButtonTapped()
}
class AdvertisementView: UIView {
// MARK: - MySubViews
//some other view...
lazy var closeButton: UIButton = {
    let buttonClose = UIButton(type: .roundedRect)
    buttonClose.translatesAutoresizingMaskIntoConstraints = false
    buttonClose.backgroundColor = .red
    buttonClose.setTitle("X", for: .normal)
    buttonClose.addAction {
        print("this is close button")
        self.delegate?.sampleButtonTapped()
    }
    return buttonClose
}()

// MARK: - Properties
var website = ""
weak var delegate: SampleButtonViewDelegate?

// MARK: - Initializers
override init(frame: CGRect) {
    super.init(frame: frame)
    clipsToBounds = true
    addCloseButton()
}
required init?(coder: NSCoder) {
    super.init(coder: coder)
    clipsToBounds = true
    addCloseButton()
}
private func addCloseButton() {
    addSubview(closeButton)
    NSLayoutConstraint.activate([
        closeButton.topAnchor.constraint(equalTo: titleLabel.topAnchor),
        closeButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 15),
        closeButton.widthAnchor.constraint(equalToConstant: 35),
        closeButton.heightAnchor.constraint(equalToConstant: 25)
    ])
}
}

in global.swift file i have added AdvertisementView to containerview--- this is global file

let adView = AdvertisementView()

func addAdvertisementView(containerView: UIView) {
//    let adView = AdvertisementView()
containerView.isHidden = false
containerView.clipsToBounds = true
containerView.addSubview(adView)
adView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    adView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 5),
    adView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -5),
    adView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 5),
    adView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -5)
])
}

and in parent class i have added view in storyboard and given its IBOutlet as adContainerView: with this code i can able to close adContainerView when i click close button

after closing if i move from messagelist view controller and come back then again i want to show whole adContainerView with adView but i am getting only adContainerView but on top adView design is not coming why?

how to show adContainerView with adView`... please guide me

class MessageList: UIViewController, SampleButtonViewDelegate {

@IBOutlet weak var adContainerView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    addAdvertisementView(containerView: adContainerView)
    adView.delegate = self
}
func sampleButtonTapped() {
    adContainerView.isHidden = true
}
}

Solution

  • When you move to messagelist view controller, the previous view was not deinit. It just only hide so all the property you set still remain there. Apple calls it View Controller Life Cycle.

    In here is adContainerView still being hidden by the func sampleButtonTapped() just like you describe

    So if you need to unhide it when you get back from another view. You show check it in viewWillAppear because the view will appear again not from initialize

    Code will be like this - You can add more condition to check if you need to unhide it

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        
        if (adContainerView.isHidden) {
            adContainerView.isHidden = false
        }
    }