不幸的是,你需要從頭開始構建控制器。雖然看起來令人生畏,但最難的部分是呈現邏輯。之後它只是將這個主類用於每個ui用例的子類,併爲這些子類中的每個子類創建自己的邏輯。
這裏有一個自定義AlertControllers您的最佳方法: https://gist.github.com/murphman300/4a56ace35d0ccdbf3a0c923b4ec7dc96
您可以使用一個主類像在這個要點文件在這裏,要注意各個部分的意見。
然後,當你繼承每種類型
//An example subclass of PopUpAlertController
class SpecificAlertControllerSubclass : PopUpAlertController {
override func set() {
super.set() //Good idea to call the super., since you might want to add basic stuff all the time, in that case you would do this in the original set() method, not here in the subclass' set() override.
//Here you add your own content.. that you instantiate in the subclass. The fact set() is being called in viewDidLoad() means you don't need to worry about calling it later on.
}
}
class TestController : UIViewController, PopUpAlertControllerDelegate {
var popup : SpecificAlertControllerSubclass?
func bringUpPopUp() {
popup = SpecificAlertControllerSubclass()
popup?.delegate = self
present(popup!, animated: true) {
print("Popup Presented")
}
}
/*
The Delegate methods you will always need to consider when using it.
*/
func popUp(controller: PopUpAlertController, didDismiss withInfo: Any?) {
if let pop = popup, controller == pop {
//checks if the delegated controller is popup..
popup = nil
let info = withInfo != nil ? String(describing: withInfo!) : "unknown"
print("Dismissed PopUp, with reason: \(info)")
}
}
func popUp(controller: PopUpAlertController, selected item: [String : Any]?) {
//Here is where you would handle the user selecting one of your options. Dismissing the popup andPresenting another controller. or if you want the popup subclass to handle the logic and the next controller, you don't call this method, but handle it in the subclass object.
}
}
因此,這將彈出相同的方式爲UIAlertControllers視圖。但是,它的外觀完全取決於你。使用這種類別時需要記住的某些事項:
- 在設置覆蓋中設置彈出視圖的初始位置。
- 在
set()
覆蓋:總是記住你要添加子視圖 popUp
視圖,而不是view
。當然,您可以將彈出窗口重命名爲 。
- 如果要更改控制器的顯示,您需要 來更改子類中的覆蓋和表示變量,而不是主類。那你可以保持一般的完整,以便快速重用,但是你的特定用例仍然是可區分的。
- 我發現覆蓋
viewDidAppear
更有用,如果在 子類級別完成,出於與前一點相同的原因。您可以在其中插入單獨的演示文稿方法。
- 要更改解僱動畫,您必須自己覆蓋 方法,而不必在覆蓋中調用super。
點3和4意味着SpecificAlertControllerSubclass
變成這樣的:
class SpecificAlertControllerSubclass : PopUpAlertController {
override func set() {
super.set() //Good idea to call the super., since you might want to add basic stuff all the time, in that case you would do this in the original set() method, not in the subclasses.
//Here you add your own content..
modalPresentationStyle = //which ever one you want
modalTransitionStyle = //which ever one you want
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .allowAnimatedContent, animations: {
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
self.popUp.alpha = 1
}) { (v) in
}
}
}
喜歡上了第二委託方法解釋了評論,你需要決定哪個控制器應該處理的行動邏輯,在我看來, ,具體取決於在按下這些操作時是否顯示新控制器,或者只是切換UI。在後面的情況下,可以使PopUpAlertController
的didSelect方法成爲可選方法。 Here's a good tutorial on delegates。
我還將popUp的alpha設置爲0,這是針對您的用例,它是一箇中心屏幕淡入效果。當然,如果你想讓彈出窗口滑入,你可以在set()
覆蓋中更改該alpha值。如果您還想在動畫中滑動幻燈片,則只需在set()
覆蓋中設置彈出窗口的初始位置,然後將其設置爲在viewDidAppear
覆蓋中出現的位置進行動畫處理。因此,從這個方面來看,這種方法確實需要你跟蹤你如何改變你的子類中的主類的屬性,但是,當你掌握它的時候,它的可定製性是非常整潔的。
從這裏,你可以做任何你想做的事情。只需記住在選擇操作時處理彈出窗口的UI過渡,以確保過渡順暢等。
至於你想要的外觀。我認爲你最好的選擇是使用帶有全長邊框的UITableView
或UICollectionView
。您應該委派您在PopUpAlertController中決定使用哪種方法,使用其各自的didSelectItemAt
方法解釋選擇方法,並調用PopUpAlertControllerDelegate
的popUp(controller: PopUpAlertController, didDismiss withInfo: Any?)
。這將允許您爲每一行設置自定義圖標。
這裏有UICollectionView一個很好的教程的編程方式: https://www.youtube.com/watch?v=3Xv1mJvwXok&list=PL0dzCUj1L5JGKdVUtA5xds1zcyzsz7HLj
所以基本上,你有顯示圖標的問題? – murphguy
如果這就是你想要做的,你不能將圖標添加到UIAlertController,因爲這些是操作系統級別的UI元素(如觸摸ID,應用程序權限請求等)。如果您希望具有更多可定製內容的Alert視圖,您將不得不自行構建。我會發佈一個答案,以您需要的方式實現這個程序化的基礎(相信我,以故事板的形式編程自定義彈出視圖更容易)。最終你會想要創建這樣的東西:https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot2.png – murphguy
我在顯示同一行上的多個項目時遇到問題,操作表看起來像它只支持每行一個動作,但我想要兩個!非常感謝! – Chris