我最近开始遇到一个问题,即弹出窗口控制器有时会显示在错误的角落。当其正常工作时,弹出源是位于右上角的按钮。这就是大多数时候发生的情况。现在,当我从特定路线“太快”着陆到此页面时,弹出窗口显示在左上角。
另一件事很奇怪,当人为地延迟加载时(例如当我设置调试点时),弹出窗口会正确显示。
因为这似乎是一个计时问题,所以我尝试将导航配置从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)
}
这听上去很奇怪,但是我之前也发生过类似的事情,并且在呈现VC时将动画设置为false时,它就解决了,
self.present(actionSheet, animated: false, completion: nil)