Swift将箭头和sourceView添加到UIAlertController

时间:2016-08-16 15:53:36

标签: ios swift uialertcontroller

我有一个UIAlertController,我正在尝试向它添加一个箭头并将sourceView更改为self.button,因此它不会出现在底部。

这是我到目前为止所得到的:

let dropdown = UIAlertController(title: "Select Room", message: nil, preferredStyle: .ActionSheet)

        let kitchen = UIAlertAction(title: "Kitchen", style: .Default) { (action) in
        }
        dropdown.addAction(kitchen)

        let livingRoom = UIAlertAction(title: "Living Room", style: .Default) { (action) in
        }
        dropdown.addAction(livingRoom)

        let bedroom = UIAlertAction(title: "Bedroom", style: .Default) { (action) in
        }
        dropdown.addAction(bedroom)

        let bathroom = UIAlertAction(title: "Bathroom", style: .Default) { (action) in
        }
        dropdown.addAction(bathroom)

        let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
        }
        dropdown.addAction(cancel)

        dropdown.modalPresentationStyle = .Popover

        dropdown.popoverPresentationController?.sourceRect = self.dropdownButton.frame
        dropdown.popoverPresentationController?.sourceView = self.dropdownButton
        dropdown.popoverPresentationController?.permittedArrowDirections = .Up

        self.presentViewController(dropdown, animated: true, completion: nil)

但箭头没有出现,UIAlertController仍然出现在底部。我做错了什么?

1 个答案:

答案 0 :(得分:0)

popoverPresentationController(以及它的设置)旨在由UIViewController presentationStyle UIModalPresentationPopover UIAlertController使用。

虽然UIViewControllerUIModalPresentationCustom的子类,但它很特殊并使用presentationStyle作为其presentationStyle。设置新UIAlertController的尝试将被忽略。

我不认为你要做的是{{1}}的预期用途。你最好的办法就是让你自己的视图控制器达到这个目的。