项目作者: maximbilan

项目描述 :
Customization of UIAlertController
高级语言: Swift
项目地址: git://github.com/maximbilan/UIAlertController-Customization.git
创建时间: 2016-02-17T11:19:43Z
项目社区:https://github.com/maximbilan/UIAlertController-Customization

开源协议:MIT License

下载


Customization of UIAlertController

Recently I’m faced with an unusual task using UIAlertController. First, add an image to some of items. And the second, add UISwitch control.

alt tag

I know it doesn’t matсh Apple design flow, but maybe someone will come in handy for resolving different tasks.

The first, how to add an image. UIAlertController or UIAlertAction has not public methods for this, but you can do this via setValue for key ‘image’. For example:

  1. alertAction.setValue(UIImage(named: "image1.png"), forKey: "image")

But you will get no good result.

alt tag

Please create a version of this image with the specified rendering mode. In our case with AlwaysOriginal.

  1. alertAction.setValue(UIImage(named: "image1.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), forKey: "image")

And see what we will get:

alt tag

The second thing, how to add a switch control?

Let’s create a new view controller and a user interface for this.

alt tag

  1. import UIKit
  2.  
  3. class SwitchAlertActionViewController: UIViewController {
  4. @IBOutlet weak var valueSwitch: UISwitch!
  5. var isSwitchOn = false
  6.  
  7. override func viewDidLoad() {
  8. super.viewDidLoad()
  9.  
  10. valueSwitch.on = isSwitchOn
  11. }
  12. }

And the last point, we need to apply this controller to UIAlertAction. The same way using a key contentViewController.

  1. let switchAlert = SwitchAlertActionViewController()
  2. switchAlert.isSwitchOn = true
  3. alertAction.setValue(switchAlert, forKey: "contentViewController")

alt tag