Alternative for xib in swift3 iOS

A

Hi all,

Lets see the simplest and best way to replace or avoid use of xib by using UIView on storyboard.

 

  1.  Go on storyboard ,select Viewcontroller Drag UIView and drop it between FirstResponder and exit button on that.
  2.  Add IBOutlet of that UIView on ViewController.swift file.
  3.  set the center, width, height for that UIView
  4.  Refer Following code :

 

——– IBOutlet ——–

@IBOutlet var View_alternativeForXib: UIView!

var view_BGDimmer = UIView()

    

——– Set the center , width, height for that UIView as well as add transperent Background view ———

 

       View_alternativeForXib.center = (UIApplication.shared.keyWindow?.center)!

          view_BGDimmer.frame = (UIApplication.shared.keyWindow?.frame)!

         View_alternativeForXib.frame.size.width = (UIApplication.shared.keyWindow?.frame.width)! – 40

         View_alternativeForXib.frame.origin.x = (UIApplication.shared.keyWindow?.frame.origin.x)! + 20

          view_BGDimmer.backgroundColor =  colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)

          view_BGDimmer.alpha = 0.3

          UIApplication.shared.keyWindow?.addSubview(view_BGDimmer)

          UIApplication.shared.keyWindow?.insertSubview(self.View_alternativeForXib, aboveSubview: self.view_BGDimmer)

          UIView.animate(withDuration: 0.1, animations: {

          }) { (completed) in

          }

 

——-  To dismiss view ——

   func dimissPopUp() {

        UIView.animate(withDuration: 0.2, animations: {

            self.view_BGDimmer.alpha = 0.0

            self.View_alternativeForXib.removeFromSuperview()

            self.view.layoutIfNeeded()

        }) { (completed) in

            self.view_BGDimmer.removeFromSuperview()

        }

    }

 

 

Thanks,

 

 

About the author

Shraddha Ingale
By Shraddha Ingale

Category