如何在 TableView 上居中弹出窗口?
How center popover on TableView?
我正在制作一个在每个单元格中都有一个按钮的表格视图,当您按下按钮时会显示一个弹出窗口,但问题是使弹出窗口在表格视图上居中,但是如果您向下移动表格视图,并且按下按钮,弹出窗口使用 select 按钮作为指南,因此弹出窗口会上升。在接下来的图片中,您可以看到我试图解释的内容。
从单元格中按下按钮时使用的代码:
func buttonPressed(sender: UIButton){
let buttonTag = sender.tag
let width = self.view.frame.midX + (self.view.frame.midX/2)
let height = info[buttonTag].nota.calculateHeight(width: width, sizeFont: 16.0) + 40
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NotaTable") as! Popever
vc.modalPresentationStyle = UIModalPresentationStyle.popover
vc.popoverPresentationController?.delegate = self
vc.preferredContentSize = CGSize(width: width, height: height + 115)
vc.width = width
let popover: UIPopoverPresentationController = vc.popoverPresentationController!
popover.delegate = self
popover.sourceView = self.view
popover.sourceRect = CGRect(x: self.view.frame.midX - (width/2), y: self.view.frame.midY - (height/2), width: width, height: height)
popover.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
present(vc, animated: true, completion:nil)
}
那么,无论按下什么按钮,如何使弹出窗口聚焦在中心?
提前致谢!
我找到了解决这个问题的方法,改为更改 popover.sourceRect
用 self.view.frame 代替 self.view.bounds
完成 de x 和 y 位置
let popover: UIPopoverPresentationController = vc.popoverPresentationController!
popover.delegate = self
popover.sourceView = self.view
popover.sourceRect = CGRect(x: (self.view.bounds.midX - (width/2)), y: (self.view.bounds.midY - (height/2)), width: width, height: height)
这里给大家留下frame和bounds的区别:
UIView 的边界是矩形,表示为相对于其自身坐标系 (0,0) 的位置 (x,y) 和大小 (width,height)。
UIView 的框架是一个矩形,表示为相对于它所包含的超级视图的位置 (x,y) 和大小(宽度,高度)。
您可以将 Table View bounds
分配给弹出框架视图。更多详情请看下方代码。
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let popOver = storyboard.instantiateViewController(withIdentifier:"MSGPopupController") as! MSGPopupController
self.addChildViewController(popOver)
popOver.view.frame = self.tableView.bounds
self.view.addSubview(popOver.view)
popOver.didMove(toParentViewController: self)
但请记住,您的 table 仍然可以滚动。与弹出窗口。要解决此问题,您必须使用类似协议。
Before opening the pop up just add self.tableView.isScrollEnabled = false
and set delegate popOver.msg_delegate = self
.
那么你的完整代码就是
func message_pop_up(message : String) {
self.tableView.isScrollEnabled = false
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let popOver = storyboard.instantiateViewController(withIdentifier:"MSGPopupController") as! MSGPopupController
popOver.msg_delegate = self
self.addChildViewController(popOver)
popOver.view.frame = self.tableView.bounds
self.view.addSubview(popOver.view)
popOver.didMove(toParentViewController: self)
}
代表class喜欢
extension Your_Current_Controller : MSGPopupDelegate {
func ok_click() {
self.tableView.isScrollEnabled = true
}
}
你的 MSGPopupController
应该和协议一样。否则,您可以根据需要更改它。
import UIKit
protocol MSGPopupDelegate {
func ok_click()
}
class MSGPopupController: UIViewController {
@IBOutlet weak var tf_title: UILabel! // This is taken from My View
@IBOutlet weak var tf_message: UILabel! // This is taken from My View
@IBOutlet weak var btn_close_ot: UIButton! // This is taken from My View
var message_text : String!
var msg_delegate : MSGPopupDelegate! = nil
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
self.showAnimation()
tf_message.text = message_text!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// This is taken from My View
@IBAction func btn_close(_ sender: UIButton) {
removeAnimation()
if msg_delegate != nil {
msg_delegate.ok_click()
}
}
func showAnimation(){
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 removeAnimation(){
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.view.removeFromSuperview()
}})
}
}
我正在制作一个在每个单元格中都有一个按钮的表格视图,当您按下按钮时会显示一个弹出窗口,但问题是使弹出窗口在表格视图上居中,但是如果您向下移动表格视图,并且按下按钮,弹出窗口使用 select 按钮作为指南,因此弹出窗口会上升。在接下来的图片中,您可以看到我试图解释的内容。
从单元格中按下按钮时使用的代码:
func buttonPressed(sender: UIButton){
let buttonTag = sender.tag
let width = self.view.frame.midX + (self.view.frame.midX/2)
let height = info[buttonTag].nota.calculateHeight(width: width, sizeFont: 16.0) + 40
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NotaTable") as! Popever
vc.modalPresentationStyle = UIModalPresentationStyle.popover
vc.popoverPresentationController?.delegate = self
vc.preferredContentSize = CGSize(width: width, height: height + 115)
vc.width = width
let popover: UIPopoverPresentationController = vc.popoverPresentationController!
popover.delegate = self
popover.sourceView = self.view
popover.sourceRect = CGRect(x: self.view.frame.midX - (width/2), y: self.view.frame.midY - (height/2), width: width, height: height)
popover.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
present(vc, animated: true, completion:nil)
}
那么,无论按下什么按钮,如何使弹出窗口聚焦在中心?
提前致谢!
我找到了解决这个问题的方法,改为更改 popover.sourceRect 用 self.view.frame 代替 self.view.bounds
完成 de x 和 y 位置let popover: UIPopoverPresentationController = vc.popoverPresentationController!
popover.delegate = self
popover.sourceView = self.view
popover.sourceRect = CGRect(x: (self.view.bounds.midX - (width/2)), y: (self.view.bounds.midY - (height/2)), width: width, height: height)
这里给大家留下frame和bounds的区别:
UIView 的边界是矩形,表示为相对于其自身坐标系 (0,0) 的位置 (x,y) 和大小 (width,height)。
UIView 的框架是一个矩形,表示为相对于它所包含的超级视图的位置 (x,y) 和大小(宽度,高度)。
您可以将 Table View bounds
分配给弹出框架视图。更多详情请看下方代码。
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let popOver = storyboard.instantiateViewController(withIdentifier:"MSGPopupController") as! MSGPopupController
self.addChildViewController(popOver)
popOver.view.frame = self.tableView.bounds
self.view.addSubview(popOver.view)
popOver.didMove(toParentViewController: self)
但请记住,您的 table 仍然可以滚动。与弹出窗口。要解决此问题,您必须使用类似协议。
Before opening the pop up just add
self.tableView.isScrollEnabled = false
and set delegatepopOver.msg_delegate = self
.
那么你的完整代码就是
func message_pop_up(message : String) {
self.tableView.isScrollEnabled = false
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let popOver = storyboard.instantiateViewController(withIdentifier:"MSGPopupController") as! MSGPopupController
popOver.msg_delegate = self
self.addChildViewController(popOver)
popOver.view.frame = self.tableView.bounds
self.view.addSubview(popOver.view)
popOver.didMove(toParentViewController: self)
}
代表class喜欢
extension Your_Current_Controller : MSGPopupDelegate {
func ok_click() {
self.tableView.isScrollEnabled = true
}
}
你的 MSGPopupController
应该和协议一样。否则,您可以根据需要更改它。
import UIKit
protocol MSGPopupDelegate {
func ok_click()
}
class MSGPopupController: UIViewController {
@IBOutlet weak var tf_title: UILabel! // This is taken from My View
@IBOutlet weak var tf_message: UILabel! // This is taken from My View
@IBOutlet weak var btn_close_ot: UIButton! // This is taken from My View
var message_text : String!
var msg_delegate : MSGPopupDelegate! = nil
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
self.showAnimation()
tf_message.text = message_text!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// This is taken from My View
@IBAction func btn_close(_ sender: UIButton) {
removeAnimation()
if msg_delegate != nil {
msg_delegate.ok_click()
}
}
func showAnimation(){
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 removeAnimation(){
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.view.removeFromSuperview()
}})
}
}