如何使用 swift 创建自定义弹出视图?
How to create a custom pop up view with swift?
所以我正在开发一个简单的应用程序,我在其中使用 table 视图。此 table 视图显示 "players" 的名称。但是为了让我在 table 视图中添加玩家,我想要一个弹出窗口 window 显示一个文本字段,您可以在其中提供名称。
现在我一直在阅读有关创建 xib 或 nib 文件的内容,但我不确定如何 "load" 弹出窗口 window。
最好的方法是什么?
看起来像这样:
您将创建一个自定义 UIView
,其中包含所有需要的相关对象,您将从控制器的 viewDidLoad() 中隐藏它。
customView.hidden = true
每当您的用户想要执行某些操作或任务时,您将其取消隐藏,一旦用户完成,然后 再次隐藏它 或 从 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()
}
看看这个 github project,它已关闭以供生产使用,它为您提供了处理(呈现和关闭)UIView 的更好方法
If you only want the player's name then use a UIAlertController
containing a textfield
在情节提要中
- 在故事板中创建一个 UIViewController 并根据您的要求进行设计。
- 将自定义 class 设置为另一个 ViewController 并将 Storyboard_ID 设置为 another_view_sid
- 创建一个新的 Cocoa Touch Class as anotherViewController and subclass of 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)
在另一个ViewController中
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()
}
})
}
这个第 3 方 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)
所以我正在开发一个简单的应用程序,我在其中使用 table 视图。此 table 视图显示 "players" 的名称。但是为了让我在 table 视图中添加玩家,我想要一个弹出窗口 window 显示一个文本字段,您可以在其中提供名称。
现在我一直在阅读有关创建 xib 或 nib 文件的内容,但我不确定如何 "load" 弹出窗口 window。
最好的方法是什么?
看起来像这样:
您将创建一个自定义 UIView
,其中包含所有需要的相关对象,您将从控制器的 viewDidLoad() 中隐藏它。
customView.hidden = true
每当您的用户想要执行某些操作或任务时,您将其取消隐藏,一旦用户完成,然后 再次隐藏它 或 从 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()
}
看看这个 github project,它已关闭以供生产使用,它为您提供了处理(呈现和关闭)UIView 的更好方法
If you only want the player's name then use a
UIAlertController
containing a textfield
在情节提要中
- 在故事板中创建一个 UIViewController 并根据您的要求进行设计。
- 将自定义 class 设置为另一个 ViewController 并将 Storyboard_ID 设置为 another_view_sid
- 创建一个新的 Cocoa Touch Class as anotherViewController and subclass of 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)
在另一个ViewController中
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()
}
})
}
这个第 3 方 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)