Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Custom UIView and display as Pop Up in Swift

I am trying to create a custom UIView and display it as a pop up in my main View using Swift.

My Custom UIView code is

class DatePopUpView: UIView {
var uiView:UIView?

override init()  {
    super.init()
    self.uiView = NSBundle.mainBundle().loadNibNamed("DatePopUpView", owner: self, options: nil)[0] as? UIView

}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
      }

required override init(frame: CGRect) {
           super.init(frame: frame)

}

}

And I am Calling it in my main view as:

 @IBAction func date_button_pressed (sender : AnyObject?) {
 var popUpView = DatePopUpView()
 var centre : CGPoint = CGPoint(x: self.view.center.x, y: self.view.center.y)

    popUpView.center = centre
    popUpView.layer.cornerRadius = 10.0
  let trans = CGAffineTransformScale(popUpView.transform, 0.01, 0.01)
    popUpView.transform = trans
    self.view .addSubview(popUpView)
    UIView .animateWithDuration(0.5, delay: 0.0, options:     UIViewAnimationOptions.CurveEaseInOut, animations: {

        popUpView.transform = CGAffineTransformScale(popUpView.transform, 100.0, 100.0)

        }, completion: {
            (value: Bool) in

    })

 }

But popUp is not Coming. I used breakpoint and noticed that value is getting assigned to my popUpView but still it is not displayed on my main View. Please Help

Please Note: I am using StoryBoard for my mainView and custom View i have made using xib.

like image 424
Shruti Avatar asked Mar 23 '15 06:03

Shruti


People also ask

How do I present UIView in Swift?

present the gray UIView like you would usually present a ViewController (appearing bottom up and user can slide down to dismiss). The bottomBar is a ContainerView and should not change by switching between the VC's, only the gray UIView which you can see in the 2nd picture.


1 Answers

try to modify your View to something like this:

class DatePopUpView: UIView {
     var uiView:UIView?
     override init()  {
         super.init()
         self.setup()
     }
     required init(coder aDecoder: NSCoder)  {
         super.init(coder: aDecoder)
         self.setup()
     }
     override init(frame: CGRect)   {
         super.init(frame: frame)
         self.setup()
     }
     setup() {
         self.uiView = NSBundle.mainBundle().loadNibNamed("DatePopUpView", owner: self, options: nil)[0] as? UIView
         self.uiView.frame = self.bounds
         self.uiView.autoresizingMask = .FlexibleWidth | .FlexibleHeight
         self.addSubview(self.uiView)
     }
}
like image 158
dragonball Avatar answered Oct 26 '22 04:10

dragonball