如何使用swift创建自定义弹出视图?

J A*_*ngo 12 popup uitableview ios swift

所以我正在开发一个简单的应用程序,在那里我使用表格视图.此表视图显示"播放器"的名称.但是为了让我在表格视图中添加玩家,我想要一个弹出窗口显示一个文本字段,您可以在其中提供名称.

现在我一直在阅读有关创建xib或nib文件的内容,但我不确定如何"加载"弹出窗口.

什么是最好的方法?

看起来像这样:

弹出视图

在此输入图像描述

Mil*_*hah 18

在故事板中

  • 在storyboard中创建一个UIViewController并根据您的要求进行设计.
  • 将自定义类设置为anotherViewController,将Storyboard_ID设置为another_view_sid
  • 创建一个新的Cocoa Touch类作为另一个ViewController和UIVIewController的子类

在viewController中

    let popvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "another_view_sid") as! anotherViewController

    self.addChildViewController(popvc)

    popvc.view.frame = self.view.frame

    self.view.addSubview(popvc.view)

    popvc.didMove(toParentViewController: self)
Run Code Online (Sandbox Code Playgroud)

在anotherViewController中

override func viewDidLoad() {
    super.viewDidLoad()
        showAnimate()
    }
}

@IBAction func Close_popupView(_ sender: Any) {
    removeAnimate()
}

func showAnimate()
{
    self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    self.view.alpha = 0.0
    UIView.animate(withDuration: 0.25, animations: {
        self.view.alpha = 1.0
        self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    })
}

func removeAnimate()
{
    UIView.animate(withDuration: 0.25, animations: {
        self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
        self.view.alpha = 0.0
    }, completion: {(finished : Bool) in
        if(finished)
        {
            self.willMove(toParentViewController: nil)
            self.view.removeFromSuperview()
            self.removeFromParentViewController()
        }
    })
}
Run Code Online (Sandbox Code Playgroud)


Lam*_*our 15

UIView需要从Controller的viewDidLoad()中创建一个包含所需的所有受尊重对象的自定义,您将隐藏它.

customView.hidden = true
Run Code Online (Sandbox Code Playgroud)

每当您的用户想要执行某些操作或任务时,您取消隐藏它,一旦用户完成,然后再次隐藏它从superView中删除.

customView.hidden = false
Run Code Online (Sandbox Code Playgroud)

下面有一些代码可以帮助您入手

    private var customView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        customView.hidden = true
    }

    private func loadCustomViewIntoController() {
         let customViewFrame = CGRect(x: 0, y: 0, witdh: view.frame.width, height: view.frame.height - 200)
         customView = UIView(frame: customViewFrame)

         view.addSubview(customView)

         customView.hidden = false

        // any other objects should be tied to this view as superView 
        // for example adding this okayButton

        let okayButtonFrame = CGRect(x: 40, y: 100, width: 50, height: 50)
        let okayButton = UIButton(frame: okayButtonFrame )

         // here we are adding the button its superView
         customView.addSubview(okayButton)

         okayButton.addTarget(self, action: #selector(self.didPressButtonFromCustomView:), forControlEvents:.TouchUpInside)

    }


    func didPressButtonFromCustomView(sender:UIButton) {
         // do whatever you want
         // make view disappears again, or remove from its superview
    }


    @IBAction func rateButton(sender:UIBarButtonItem) {
        // this barButton is located at the top of your tableview navigation bar
        // when it pressed make sure you remove any other activities that were on the screen, for example dismiss a keyboard 

          loadCustomViewIntoController()
    }
Run Code Online (Sandbox Code Playgroud)

看看这个github项目,它已经关闭生产就绪,它为你提供了一个更好的方式来处理(呈现和解雇)UIViews

如果您只想要播放器的名称,则使用UIAlertController包含文本字段的名称