2016-08-16 108 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的預期用途。你最好打賭是讓你自己的視圖控制器來達到這個目的。