【问题标题】:Center Swift Pop-Over on screen在屏幕上居中 Swift 弹出窗口
【发布时间】:2016-07-01 13:41:16
【问题描述】:

我目前正在开发一个应用程序,该应用程序要求用户配置文件在按下用户图片时显示为弹出窗口。我设法使用 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”属性来实现所需的行为,但我还没有弄清楚。

【问题讨论】:

标签: ios swift user-interface uipopovercontroller


【解决方案1】:

设置宽度和高度后设置frame

  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)

【讨论】:

猜你喜欢
  • 2011-05-03
  • 2012-12-09
  • 2010-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多