ios - swift popover
신속하게 popoverview를 집중시키는 방법 (6)
iOS8에서는 너비와 높이를 계산할 때 self.view.frame
을 사용할 필요가 없습니다.
다음과 같은 방법으로 대화 상자 높이와 너비를 지정할 수 있습니다.
override func viewDidLoad() {
var frameSize: CGPoint = CGPointMake(UIScreen.mainScreen().bounds.size.width*0.5, UIScreen.mainScreen().bounds.size.height*0.5)
self.preferredContentSize = CGSizeMake(frameSize.x,frameSize.y);
}
편집 됨 :
아래와 같이 contentSizeForViewInPopover
를 설정할 수도 있습니다.
self.contentSizeForViewInPopover = CGSizeMake(320.0, 360.0)
이게 도움이되는지 아닌지 알려주시겠습니까?
화살표가없는 popoverview (대화 상자)를 표시하는 다음 코드는 정상적으로 작동합니다. 유일한 문제는 대화 상자가 왼쪽 상단 (IPad)에 표시된다는 것입니다. 나는 화면에서보기를 중심에 놓고 싶습니다.
다음 코드를 변경하거나 추가 할 대상은 무엇입니까? :
func show_help(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Help") as! UIViewController
controller.modalPresentationStyle = UIModalPresentationStyle.popover
let popoverPresentationController = controller.popoverPresentationController
// result is an optional (but should not be nil if modalPresentationStyle is popover)
if let _popoverPresentationController = popoverPresentationController {
// set the view from which to pop up
_popoverPresentationController.sourceView = self.view;
_popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.allZeros;
// present (id iPhone it is a modal automatic full screen)
self.presentViewController(controller, animated: true, completion: nil)
}
}
추가 정보
내 viewcontroller에 연결된 내보기에서 나는 preffered 크기를 다음과 같이 설정합니다.
override func viewDidLoad() {
let dialogheigth:CGFloat = self.view.frame.height * 0.5;
let dialogwidth:CGFloat = self.view.frame.width * 0.5;
self.preferredContentSize = CGSizeMake(dialogwidth,dialogheigth);
}
popover를위한 소스 rect를 제공해야합니다.
사과 문서에서 : 소스 rect는 popover를 앵커 할 지정된 뷰의 사각형입니다. 이 속성을 sourceView 속성과 함께 사용하여 Popover의 기준 위치를 지정합니다.
귀하의 경우에는
_popoverPresentationController.sourceView = self.view;
더하다:
_popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0)
그것은 트릭을 할 것입니다!
만약 누군가가 도움이된다면 UIViewController에 확장 기능을 만들었습니다.
extension UIViewController{
func configureAsPopoverAndPosition(withWidthRatio widthRatio:CGFloat,
heightRatio:CGFloat){
modalPresentationStyle = .popover
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
let popover = popoverPresentationController
popover?.sourceView = self.view
popover?.permittedArrowDirections = [UIPopoverArrowDirection(rawValue: 0)]
preferredContentSize = CGSize(width: (screenWidth * widthRatio),
height: (screenHeight * heightRatio))
popover?.sourceRect = CGRect(x: view.center.x,
y: view.center.y,
width: 0,
height: 0)
}
}
용법:
if UIDevice.current.userInterfaceIdiom == .pad{
yourViewController.configureAsPopoverAndPosition(withWidthRatio: 0.7 /*Make view controller width 70 % of screen width*/,
heightRatio: 0.7/*Make view controller height 70 % of screen height*/)
}
그러면 화면 중앙에 팝업 창이 표시됩니다.
스위프트 3 (Xcode 8, iOS 9)의 또 다른 방법은 다음과 같습니다.
어딘가에서 전화 :
self.performSegue(withIdentifier: "showPopupSegue", sender: yourButton)
segue가 실행되기 전에 호출되는 함수 :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let popoverPresentationController = segue.destination.popoverPresentationController {
let controller = popoverPresentationController
controller.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
controller.sourceView = self.view
controller.sourceRect = CGRect(x: UIScreen.main.bounds.width * 0.5 - 200, y: UIScreen.main.bounds.height * 0.5 - 100, width: 400, height: 200)
segue.destination.preferredContentSize=CGSize(width: 400, height: 200)
}
}
Storyboard segue의 Kind 속성을 "Popover로 제공"으로 설정하고 Anchor 속성을 이전보기 컨트롤러의 모든보기로 설정해야합니다.
신속한 구현 4 :
popover.popoverPresentationController?.sourceRect = CGRect(x: view.center.x, y: view.center.y, width: 0, height: 0)
popover.popoverPresentationController?.sourceView = view
popover.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
Swift 4 implementation for center Popover controller
let navigationController = UINavigationController(rootViewController: controller)
navigationController.modalPresentationStyle = .popover
navigationController.modalPresentationStyle = UIModalPresentationStyle.popover
let popover = navigationController.popoverPresentationController
controller.preferredContentSize = CGSize(width:500,height:600) //manage according to Device like iPad/iPhone
popover?.delegate = self
popover?.sourceView = self.view
popover?.sourceRect = CGRect(x: view.center.x, y: view.center.y, width: 0, height: 0)
popover?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
self.present(navigationController, animated: true, completion: nil)