ios 如何快速创建自定义弹出视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33987442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:18:58  来源:igfitidea点击:

How to create a custom pop up view with swift?

iosswiftuitableviewpopup

提问by J Arango

So I am working on a simple app, where I use a table view. This table view displays the name of "players". But in order for me to add players in the table view, I want a pop up window to be displayed with a text field where you provide the name.

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

Now I have been reading about creating a xib or nib file, but I am not sure how to "load" the pop up window.

现在我一直在阅读有关创建 xib 或 nib 文件的信息,但我不确定如何“加载”弹出窗口。

What's the best approach to this?

最好的方法是什么?

Looks like this:

看起来像这样:

Pop up View

弹出视图

enter image description here

enter image description here

回答by Mili Shah

In storyboard

在故事板中

  • In storyboard create one UIViewController and design it according to your requirement.
  • Set custom class as anotherViewController and Storyboard_ID as another_view_sid
  • Create one new Cocoa Touch Class as anotherViewController and subclass of UIVIewController
  • 在故事板中创建一个 UIViewController 并根据您的要求进行设计。
  • 将自定义类设置为 anotherViewController 并将 Storyboard_ID 设置为 another_view_sid
  • 创建一个新的 Cocoa Touch 类作为 anotherViewController 和 UIVIewController 的子类

In 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)

In 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()
        }
    })
}

回答by Lamour

you'd create an custom UIViewwith all respected object needed, from your Controller's viewDidLoad() you'll hide it.

您将UIView使用所需的所有受尊敬的对象创建一个自定义,从您的控制器的 viewDidLoad() 中您将隐藏它。

customView.hidden = true

Whenever your user wants to perform some action or task, you unhide it and once the user finished then hide it againor remove from the superView.

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

customView.hidden = false

Below there is some code to help you start

下面有一些代码可以帮助您开始

    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()
    }

Check it out this github project, It's closed to be production ready, it gives you a better way to deal (present and dismiss) with UIViews

看看这个github 项目,它已关闭生产准备,它为您提供了一种更好的方式来处理(呈现和关闭)UIViews

If you only want the player's name then use a UIAlertControllercontaining a textfield

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

回答by huync

This 3rd party EzPopup (https://github.com/huynguyencong/EzPopup) can help you show it up. Just design your popup in storyboard, then init it.

这个 3rd 方 EzPopup ( https://github.com/huynguyencong/EzPopup) 可以帮你展示出来。只需在故事板中设计您的弹出窗口,然后初始化它。

// init YourViewController
let contentVC = ...

// Init popup view controller with content is your content view controller
let popupVC = PopupViewController(contentController: contentVC, popupWidth: 100, popupHeight: 200)

// show it by call present(_ , animated:) method from a current UIViewController
present(popupVC, animated: true)