我目前正在开发一个应用程序,该应用程序要求在按下用户图片时将用户配置文件显示为弹出窗口。我设法使用 UIPopoverPresentationControllerDelegate 实现了这种行为,但我希望你能帮助我在屏幕中间输入这个弹出窗口。
我的代码如下:
func presentProfile(sender: UIButton){
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("profileVC") as! ProfileViewController
vc.profilePicture = (self.contentIdToImage[content.hostId as! String]?.image)!
vc.modalPresentationStyle = UIModalPresentationStyle.Popover
let height = self.view.bounds.size.height
let width = self.view.bounds.size.width
vc.preferredContentSize = CGSizeMake(width * 0.8, height * 0.8)
if let presentationController = vc.popoverPresentationController {
presentationController.delegate = self
presentationController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
presentationController.sourceView = sender
self.presentViewController(vc, animated: true, completion: nil)
}
}
谢谢
编辑:
根据我收到的第一个答案,我开始尝试使用 CGRect,我认为使用 UIPopoverPresentationController 的“sourceRect”属性可以实现所需的行为,但我还没有弄明白。
最佳答案
设置宽度和高度后设置框架
let height = self.view.bounds.size.height
let width = self.view.bounds.size.width
vc.preferredContentSize = CGSizeMake(width * 0.8, height * 0.8)
vc.frame = CGRect(x: CGRectGetMidX(width - vc.bounds.width / 2), y: CGRectGetMidY(height - vc.bounds.height/2), width: width * 0.8, height: height * 0.8)
关于ios - 在屏幕上居中 Swift Pop-Over,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38146993/