使用自定义UIView在Swift中创建一个PopUp

时间:2015-03-23 09:42:20

标签: ios swift

我正在尝试创建一个自定义UIView并使用Swift在我的主视图中将其显示为弹出窗口。

我的自定义UIView代码

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)

}

}

我在主视图中将其称为:

 @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

    })

 }

但popUp并非即将到来。我使用断点并注意到该值已分配给我的popUpView但仍然没有显示在我的主视图上。请帮忙

请注意:我使用StoryBoard作为我的mainView和我使用xib进行的自定义View。

1 个答案:

答案 0 :(得分:0)

如果没有关于您尝试做什么的其他说明,我可以建议类似下面的代码吗?基本上,您可以使用视图(或任何其他控件)的.hidden功能来显示/隐藏视图。您可以使用布局编辑器设置要弹出的视图的大小和位置。

        import UIKit

    class ViewController: UIViewController {
        var popped = false
        var popupBtnTitle = "Show Popup"
        @IBAction func popupButton(sender: UIButton) {
            popped = !popped
            anotherView.hidden = !popped
            popupBtnTitle = popped  ? "Hide Popup" : "Show Popup"
            popupButtonOutlet.setTitle(popupBtnTitle, forState: UIControlState.Normal)
        }
        @IBOutlet weak var popupButtonOutlet: UIButton!
        @IBOutlet weak var anotherView: UIView!
        override func viewDidLoad() {
            super.viewDidLoad()
            popped = false
            anotherView.hidden = !popped
            popupButtonOutlet.setTitle(popupBtnTitle, forState: UIControlState.Normal)


            // Do any additional setup after loading the view.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

    }