ios - 在屏幕上居中 Swift Pop-Over

标签 ios swift user-interface uipopovercontroller

我目前正在开发一个应用程序,该应用程序要求在按下用户图片时将用户配置文件显示为弹出窗口。我设法使用 UIPopoverPresentationControllerDelegate 实现了这种行为,但我希望你能帮助我在屏幕中间输入这个弹出窗口。

current and desired behaviour

我的代码如下:

    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/

相关文章:

ios - 订阅到期时 Apple 订阅回调不会通知我们(沙盒)

ios - 自动锁定 ios 设备,NSTimer 和 scheduledTimerWithTimeInterval

jquery - 如何处理 jQuery 中的多个元素?

c# - 是否可以在 Java 项目中使用 .Net GUI?

带有 GUI 的 Javax.swing.timer (Eclipse)

ios - 函数退出后不保留对数组的更改

ios - 从 UITableViewCell 获取数据

swift - 类型 'AVCaptureDevice' 没有成员 'defaultDevice'

swift - 使用 query.whereKey 在 Swift 中解析查询

ios - AVPlayer:在 Swift 中播放 “on the fly” 转码音频文件