Search code examples
iosswiftuser-interfaceforeground

I want to implement grey foregound with instruciton in my ios App


I want to implement something like this in my ios app :enter image description here

I want a transparent foreground with a warning message similar to "please enable Bluetooth to continue using this app" in my ios App. How can I implement a similar kind of behavior in my app?


Solution

  •  var vc: BannerViewController!
    
        func addBanner(){
            vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "BannerViewController")
            UIApplication.shared.keyWindow?.addSubview(vc.view)
            let sv = vc.view.superview!
            vc.view.translatesAutoresizingMaskIntoConstraints = false
            let constrainst = [
                vc.view.topAnchor.constraint(equalTo: sv.topAnchor),
                vc.view.leadingAnchor.constraint(equalTo: sv.leadingAnchor),
                vc.view.bottomAnchor.constraint(equalTo: sv.bottomAnchor),
                vc.view.trailingAnchor.constraint(equalTo: sv.trailingAnchor)
            ]
            vc.view.isUserInteractionEnabled = true
            vc.view.backgroundColor = UIColor.red
            NSLayoutConstraint.activate(constrainst)
        }
    
        func removeBanner(){
            vc.view.removeFromSuperview()
            vc = nil
        }
    

    The above functions can help you achieve what you want. basically design your banner in storyboard. then have a variable at class level. when the bluetooth is turned off in that callback, call the addBanner function and in the callback function of bluetooth turned on call the removeBanner function.