残念ながら、あなたがゼロからコントローラを構築する必要があります。それは難しいように見えるかもしれませんが、最も難しい部分はプレゼンテーションロジックです。その後で、このメインクラスのユースユースケースをサブクラス化し、それらのサブクラスのそれぞれにビルドに関する独自のロジックを与えることが問題になります。あなたは、各パートのコメントに注意を払って、ここではこの主旨ファイル内の1つのようにメインクラスを使用 https://gist.github.com/murphman300/4a56ace35d0ccdbf3a0c923b4ec7dc96
:
はここでカスタムAlertControllersするための最良のアプローチです。あなたは、各タイプ
//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と同じようにビューが開かれます。しかし、それの外観は完全にあなたに依存します。
- 設定オーバーライドでのpopUpビューの初期位置を設定します。
set()
オーバーライド: popUp
ビューにサブビューを追加していますが、view
ではなく常にサブビューを追加しています。もちろん、popUpの名前を に変更することもできます。
- コントローラーのプレゼンテーションを変更するには、メインクラスではなく、サブクラスのオーバーライドとプレゼンテーション変数を変更する必要があります( )。その場合、一般的なものをそのまま使用して素早く再利用できますが、 の特定のユースケースは引き続き区別できます。
- 私は、以前のポイントと同じ理由で、サブクラスのレベル で行われている場合
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のアルファを0に設定しました。これは、センター画面のフェードイン効果の使用例です。もちろん、ポップアップをスライドインしたい場合は、set()
オーバーライドでそのアルファ値を変更することができます。アニメーションのスライドが必要な場合は、set()
のオーバーライドでpopUpの初期位置を設定し、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にアイコンを追加することはできません。それらはOSレベルのUI要素(Touch ID、アプリ許可要求など)ですからです。よりカスタマイズ可能なコンテンツを持つアラートビューが必要な場合は、独自にビルドする必要があります。私はこれをプログラマチックに達成するために必要な基本的な答えを投稿します(私を信頼して、プログラマチックにストーリーボードでカスタムポップアップビューを作成するほうがずっと簡単です)。最終的には次のようなものを作成したいと思うでしょう:https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot2.png – murphguy
同じ行に複数のアイテムを表示するのに問題があります。行ごとに1つのアクションしかサポートしていませんが、2つが必要です!本当にありがとう! – Chris