Search code examples
iosswiftuibuttonsubviewaddsubview

why Button SubView will not go to the back in swift 4?


I have a view class with xib file so I want to use this view at the background of the button so I used subview but the problem is that the subview of the button will be inFront of the button so I can't click button any more I used UI Debugging mode and I realized that the button is at the back of the view so I used send to back in my codes But still the view is inFront of the Button !

 let adsView = videoAdsView()
func adsOutlet() {
    self.adsView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
    self.showAdsOutlet.addSubview(adsView)
    self.showAdsOutlet.sendSubview(toBack: self.adsView)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    adsOutlet()
}

Solution

  • put following code at the end of adsOutlet method:

    self.adsView.isUserInteractionEnabled = false