如何在TableView上中心弹出?

时间:2017-07-07 00:46:19

标签: swift xcode swift3 popover

我在每个单元格中创建一个带有按钮的tableview,当你按下按钮显示一个弹出窗口时,但问题是使popover以tableview为中心,但是如果你沿着tableView查看,按下弹出窗口按钮作为指导选择按钮,弹出窗口向上。在接下来的图像中,您可以看到我想要解释的内容。

enter image description here

从单元格中按下按钮时使用的代码:

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专注于中心?

谢谢你!

2 个答案:

答案 0 :(得分:2)

我找到了解决这个问题的方法,改为改变popover.sourceRect  完成de x和y位置,self.view.frame替换为self.view.bounds

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)

这里我给你留下框架和边界之间的区别:

UIView的边界是矩形,表示为相对于其自身坐标系(0,0)的位置(x,y)和大小(宽度,高度)。

UIView的框架是矩形,表示为相对于其所包含的超级视图的位置(x,y)和大小(宽度,高度)。

I got it from here

答案 1 :(得分:0)

您可以将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)

但是请记住,您的表格仍然可以滚动。与弹出。要消除此问题,您必须使用类似的协议。

  

在打开弹出窗口之前,只需添加self.tableView.isScrollEnabled = false并设置委托人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)
}

类似的委托类

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

希望这对您有帮助