Swift - 弹出式控制器根据加载时间显示在错误的角落

Swift - Popover controller shows up on wrong corner depending on load time

提问人:paco8 提问时间:6/10/2020 更新时间:6/10/2020 访问量:171

问:

我最近开始遇到一个问题,即弹出框控制器有时会出现在错误的角落。当它正常工作时,弹出框源是右上角的一个按钮。这是最常发生的事情。现在,当我从特定路线“太快”地登陆此页面时,弹出窗口会显示在左上角。

另一件奇怪的事情是,当加载被人为延迟时(例如当我为调试设置断点时),弹出框会正确显示。

由于这似乎是一个计时问题,我尝试将导航配置从 viewDidLoad 移动到 viewDidAppear,但这不起作用。

此函数是从 viewDidLoad 调用的:

func configureNav() {

        if canAddDoc == true {
            let customButton = UIButton(type: .system)
            customButton.setImage(with: .add, tintColor: UIColor.white, imageEdgeInsets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
            customButton.addTarget(self, action: #selector(showAddDocSheet), for: .touchUpInside)
            addButton = UIBarButtonItem(customView: customButton)
        }

        shareButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.action,
                                      target: self,
                                      action: #selector(shareButtonPressed))
        navigationItem.rightBarButtonItems = (addButton == nil) ? [shareButton!] : [shareButton!, addButton!]

        @objc func showAddDocSheet() {
            guard let addButton = addButton else { return }
            self.displayActionSheet(sourceButton: addButton)
        }

    }

func displayActionSheet(sourceButton: UIBarButtonItem) {

    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

     //Action sheet code goes here


    if let popoverController = actionSheet.popoverPresentationController {
        popoverController.barButtonItem = sourceButton
        popoverController.sourceView = sourceButton.customView
        popoverController.permittedArrowDirections = .up
    }

    self.present(actionSheet, animated: true, completion: nil)

}
Swift iPad UIPopOverPresentationController

评论

0赞 Andreas Oetjen 6/10/2020
“特定路线”是什么意思?您也可以尝试打印/调试错误情况下的帧,以检查是否未对齐。sourceViewsourceButton.customView

答:

0赞 Adham Khaireddin 6/10/2020 #1

这听起来可能是一个奇怪的解决方案,但之前我遇到过类似的事情,并且在显示 VC 时将动画设置为 false 时解决了这个问题,

self.present(actionSheet, animated: false, completion: nil)

评论

0赞 Adham Khaireddin 6/10/2020
我明白了,你的popOverVC工作正常,只有当你从特定路径降落在它上面时,它才会被定位在错误的地方?