2017-07-07 58 views
1

我使用每個單元格中的按鈕製作tableview,當您按下按鈕時顯示彈出窗口,但問題是使popover居中在tableview上,但是如果您沿着tableView並按下按鈕popover用作指南選擇按鈕,所以popover上升。在接下來的圖片中,您可以看到我想要解釋的內容。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) 

} 

那麼,如何讓我的酥料餅的關注中心,無論什麼按鈕按下?

感謝提前!

回答

0

我覺得解決這個方式,改變popover.sourceRect,而不是完成德x和與self.view.frame y位置替換爲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

相關問題