2017-04-18 60 views
1

我想修改橫向模式下popover的高度,但它只能在縱向模式下工作。設置橫向模式下popover的高度

我希望它的高度等於screenSize.height * 0.7,但它不適用於我的代碼下面。

enter image description here

這裏是我的代碼:

if let orientation = UIDevice.current.value(forKey: "orientation") as? Int { 
     let diamondViewController = DiamondViewController() 
     diamondViewController.mode = .buyDiamondPopup 
     diamondViewController.resetBackgroundColor = { 
      self.view.backgroundColor = .clear 
     } 

     let screenSize = UIScreen.main.bounds 
     if orientation == 3 { // LandscapeRight 
      diamondViewController.preferredContentSize = CGSize(width: screenSize.width * 0.6, height: 
       screenSize.height * 0.7) 
     } else { 
      diamondViewController.preferredContentSize = CGSize(width: screenSize.width - 60, height: 
       min(screenSize.height - 180, CGFloat(5 * 70 + 110))) 
     } 
     diamondViewController.modalPresentationStyle = .popover 
     if let popover = diamondViewController.popoverPresentationController { 
      popover.permittedArrowDirections = .init(rawValue: 0) 
      popover.sourceView = self.view 
      popover.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) 
      popover.delegate = self 
      self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5) 
      self.present(diamondViewController, animated: true, completion: nil) 
     } 
    } 

...

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { 
    return .none 
} 

回答

2

diamondViewController.preferredContentSize = CGSize(width: screenSize.width * 0.6, height: screenSize.height * 0.7) 

相反試試這個下面

diamondViewController.view = CGRect(x: diamondViewController.view.frame.origin.x ,y: diamondViewController.view.frame.origin.y ,width: screenSize.width * 0.6, height: screenSize.height * 0.7) 

如果它沒有幫助,那麼讓我知道。

+1

它不適用於iPhone 7+ – Khuong

0

您是否嘗試過使用「Vary for traits」工具?它允許您根據設備方向應用不同的約束條件。可以在故事板右下角靠近不同約束選項的位置找到它。

相關問題