我有以下代码显示无箭,其中工程罚款popoverview(对话)。唯一的问题是,该对话框中左上方总览所示。我想中心屏幕上的视图。
是什么改变或添加在我下面的代码? :
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予设定的优选尺寸是这样的:
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);
}
添加的代码下面提到线,使其中心。
popoverController.popoverPresentationController?.sourceView = view
popoverController.popoverPresentationController?.sourceRect = view.bounds
下面是使用斯威夫特3的实现
let popover = storyboard?.instantiateViewController(withIdentifier: "popover") as! PopoverVC
popover.modalPresentationStyle = UIModalPresentationStyle.popover
popover.popoverPresentationController?.backgroundColor = UIColor.green
popover.popoverPresentationController?.delegate = self
popover.popoverPresentationController?.sourceView = self.view
popover.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popover.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
self.present(popover, animated: true)
基于伊什特万的answer
斯威夫特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)
对于夫特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)
}
}
记住故事板SEGUE的类属性为“表现为酥料饼”和锚属性设置为以前的视图控制器的任何视图。
基本上包括三个步骤(iOS的8):
1.-目前认为:
比方说,你要显示的自定义视图,要求评论的用户。这里loadNibForRate()
返回从笔尖加载RateDialog
实例的功能,但你可以在这里使用您希望找到您UIViewController
任何方式
private static func presentCustomDialog(parent: RateDialogParent) -> Bool {
/// Loads the rateDialog from its xib, handled this way for further customization if desired
if let rateDialog = loadNibForRate() {
rateDialog.modalPresentationStyle = UIModalPresentationStyle.Popover
rateDialog.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
let x = parent.view.center
let sourceRectX : CGFloat
//Here we check for the orientation of the device, just to know if we are on an iPad
let maximumDim = max(UIScreen.mainScreen().bounds.height, UIScreen.mainScreen().bounds.width)
if maximumDim == 1024 { //iPad
sourceRectX = x.x
}else {
sourceRectX = 0
}
rateDialog.popoverPresentationController?.sourceView = parent.view
rateDialog.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros
rateDialog.popoverPresentationController?.sourceRect = CGRectMake(sourceRectX, x.y, 0, 0)
rateDialog.popoverPresentationController?.popoverLayoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
rateDialog.popoverPresentationController?.delegate = parent
rateDialogParent = parent
callFunctionAsync() {
parent.presentViewController(rateDialog, animated: true, completion: nil)
}
return true
}
return false
}
2:如果我们旋转我们的设备,那么酥料饼不知道往哪里祭出,除非我们有这个父
RateDialogParent
public class RateDialogParent: UIViewController, UIPopoverPresentationControllerDelegate {
/**
This function guarantees that the RateDialog is alwas centered at parent, it locates the RateDialog's view by searching for its tag (-555)
*/
public func popoverPresentationController(popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverToRect rect: UnsafeMutablePointer<CGRect>, inView view: AutoreleasingUnsafeMutablePointer<UIView?>) {
if popoverPresentationController.presentedViewController.view.tag == RateDialog.thisViewTag {
let x = popoverPresentationController.presentingViewController.view.center
let newRect = CGRectMake(x.x, x.y, 0, 0)
rect.initialize(newRect)
}
}
}
3.-你
RateDialog
应该有一个标签设置好的,这只是为了避免不必要的重新定位,如果popovers有更多的从你的RateDialogParent
呈现一个
class RateDialog: UIViewController {
@IBOutlet weak var reviewTitle: UILabel!
@IBOutlet weak var reviewMessage : UILabel!
@IBOutlet weak var cancelButtonTitle: UIButton!
@IBOutlet weak var remindButtonTitle : UIButton!
@IBOutlet weak var rateButtonTitle : UIButton!
/// For being able to locate this view
static let thisViewTag = -555
override func viewDidLoad() {
super.viewDidLoad()
//sets the tag to identify this view
self.view.tag = RateDialog.thisViewTag
}
}
斯威夫特4实施中心酥料饼控制器
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. .y, width: 0, height: 0)
popover?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
self.present(navigationController, animated: true, completion: nil)
在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)
让我知道,这有助于与否?
在情况下,如果有帮助的人,我已经创建了一个的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*/)
}
这将显示在屏幕中央的酥料饼。
下面是简单的解决方案:
乘坐公共变量var popover
var popover: UIPopoverPresentationController?
目前YourViewController
如酥料饼,用popover?.sourceRect
如下所述。
let storyboard: UIStoryboard = UIStoryboard(name: "YOUR_STORYBOARD", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "YOUR_IDENTIFIER") as! YourViewController
let navController = UINavigationController(rootViewController: vc)
navController.modalPresentationStyle = UIModalPresentationStyle.popover
popover = yourController.popoverPresentationController!
popover?.sourceRect = CGRect(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY, width: 0, height: 0)
popover?.sourceView = self.view
popover?.delegate = self
popover?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
vc.preferredContentSize = CGSize(width: width, height: height)
self.present(navController, animated: true, completion: nil)
使用viewWillTransition
的视图过渡风景和肖像。
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
popover?.sourceRect = CGRect(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY, width: 0, height: 0)
}
这会给你酥料饼中心在横向和纵向排列的屏幕。许多灵活的同时使用iPad的拆分视图。