【问题标题】:PopOver view controller covers full screenPopOver 视图控制器覆盖全屏
【发布时间】:2017-05-19 21:25:31
【问题描述】:

这是我的 PopOver 代码。我有两个视图控制器。我在 Messaging View 控制器中展示 PopOver,需要通过管道传输的视图控制器是 PreferencesView 控制器。 Storyboard Id 也是相同的 Preferences View 控制器。 popOver 是成功的,但总是覆盖全屏。即使 UIModalPresentationStyle.None。我在这里做错了什么?

 func optionClicked(sender:UIBarButtonItem)
{
    print(")show set preference and set reminder option")


    let preferenceAction: UIAlertAction = UIAlertAction(title: "Set preferences", style: .Default) { action -> Void in
        self.optionChoosed(true)
        }
    let reminderAction: UIAlertAction = UIAlertAction(title: "Set reminder", style: .Default) { action -> Void in
        self.optionChoosed(false)
    }
    let actionSheetController: UIAlertController = UIAlertController(title: kAlertTitle, message: "What you want to do?", preferredStyle: .ActionSheet)

    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
    }
    actionSheetController.addAction(preferenceAction)
    actionSheetController.addAction(reminderAction)
    actionSheetController.addAction(cancelAction)
    self.presentViewController(actionSheetController, animated: true, completion: nil)
}

func optionChoosed(isSetPreference:Bool)
{


    if(isSetPreference)
    {
    print("Set preference")

        let storyboard : UIStoryboard = UIStoryboard(name: "Messaging", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("PreferencesViewController") as! PreferencesViewController
        vc.modalPresentationStyle = UIModalPresentationStyle.Popover
        let popover: UIPopoverPresentationController = vc.popoverPresentationController!
        popover.barButtonItem?.action = "isSetPreference"
        popover.delegate = self
        presentViewController(vc, animated: true, completion:nil)


    }

    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None

    }
    func dismiss() {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

【问题讨论】:

  • 从哪里调用 optionChoosed ?并在您单击栏按钮时显示 PopOver?
  • 是的,我在栏按钮中有两个操作
  • 你能显示完整的代码吗?
  • 我应该发布什么代码
  • 我的意思是从哪里调用 optionChosed?显示调用 optionChoosed 的代码。

标签: ios swift


【解决方案1】:

我猜你是在执行故事板中的 segue。如果您这样做,请从情节提要中删除 segue 并为条形按钮项创建一个操作并将此代码放在那里。

    let storyboard : UIStoryboard = UIStoryboard(name: "Messaging", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("PreferencesViewController") as! PreferencesViewController
    vc.modalPresentationStyle = UIModalPresentationStyle.Popover
    let popover: UIPopoverPresentationController = vc.popoverPresentationController!
    vc.preferredContentSize = CGSize(width: 200, height: 200)
    popover.barButtonItem = sender as? UIBarButtonItem
    popover.delegate = self
    presentViewController(vc, animated: true, completion:nil)

【讨论】:

  • 你必须在函数中传递它。 self.optionChoosed(true,sender) 和函数签名 func optionChoosed(isSetPreference:Bool, sender: UIBarButtonItem)
  • 'NSInvalidArgumentException',原因:'-[UIButton _viewForPresenting]:无法识别的选择器发送到实例 0x7ff933a29f60' 错误使应用程序崩溃
【解决方案2】:

在调用popover的地方实现这个函数

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}

【讨论】:

    【解决方案3】:

    尝试添加

    vc.preferredContentSize = CGSize(width: 200, height: 200)
    

    希望对你有帮助

    【讨论】:

    • 你还能添加 popover.sourceView = base 和 popover.sourceRect = base.bounds
    • 如果你使用按钮改变他们像这样
    • popover.sourceView = button 和 popover.sourceRect = button.bounds
    猜你喜欢
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    相关资源
    最近更新 更多