【问题标题】:How center popover on TableView?如何在 TableView 上居中弹出框?
【发布时间】:2017-07-07 00:46:19
【问题描述】:

我在每个单元格中都制作了一个带有按钮的表格视图,当您按下按钮时会显示一个弹出框,但问题是使弹出框位于表格视图的中心,但是如果您按下表格视图并按下按钮弹出框用作选择按钮的指南,因此弹出框向上。在接下来的图片中,您可以看到我试图解释的内容。

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

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)

}

那么,无论按下什么按钮,如何让弹出框聚焦在中心?

提前感谢!

【问题讨论】:

    标签: swift xcode swift3 popover


    【解决方案1】:

    我找到了解决这个问题的方法,改为更改 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) 和大小 (width,height)。

    I got it from here

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-04
        • 2014-10-20
        • 2011-05-03
        相关资源
        最近更新 更多