2016-08-16 105 views
0

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

这是我走到这一步:

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

您只能在iPad上看到箭头。在iPhone上,它总是全屏,没有箭头。 – rmaddy

回答

0

popoverPresentationController(以及它的设置)旨在供UIViewController使用,他们的presentationStyleUIModalPresentationPopover

虽然UIAlertControllerUIViewController的子类别,但是它是特殊的并且使用UIModalPresentationCustom,因为它的presentationStyle。尝试设置新的presentationStyle将被忽略。

我不认为你想要做的是UIAlertController的预期用途。你最好打赌是让你自己的视图控制器来达到这个目的。