首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用swift创建自定义弹出视图?

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

Stack Overflow用户
提问于 2015-11-30 04:06:39
回答 3查看 68.8K关注 0票数 18

因此,我正在开发一个简单的应用程序,其中我使用了表视图。此表视图显示了"players“的名称。但为了让我在表视图中添加球员,我希望显示一个弹出窗口,其中包含一个文本字段,您可以在其中提供名称。

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

解决这个问题的最好方法是什么?

看起来像这样:

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-11-30 04:11:35

你需要用所有需要的对象创建一个定制的UIView,然后在你的控制器的viewDidLoad()中隐藏它。

代码语言:javascript
运行
复制
customView.hidden = true

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

代码语言:javascript
运行
复制
customView.hidden = false

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

代码语言:javascript
运行
复制
    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()
    }

看看这个github project,它已经关闭,准备投入生产了,它为您提供了处理(呈现和取消) UIViews的更好方法

如果您只想要球员的名字,则使用包含文本字段

UIAlertController

票数 17
EN

Stack Overflow用户

发布于 2017-03-23 19:32:55

情节提要中的

在故事板中创建一个

  • 并根据您的UIVIewController

自定义类anotherViewController和Storyboard_ID设计它一个新的可可触摸类作为anotherViewController和UIVIewController的子类

viewController中的

代码语言:javascript
运行
复制
    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)

anotherViewController中的

代码语言:javascript
运行
复制
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()
        }
    })
}
票数 20
EN

Stack Overflow用户

发布于 2018-06-08 17:16:02

此第三方EzPopup (https://github.com/huynguyencong/EzPopup)可以帮助您展示它。只需在故事板中设计弹出窗口,然后初始化它。

代码语言:javascript
运行
复制
// 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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33987442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档