Giter VIP home page Giter VIP logo

uialertcontroller-customization's Introduction

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:

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.

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

import UIKit

class SwitchAlertActionViewController: UIViewController {
  @IBOutlet weak var valueSwitch: UISwitch!
  var isSwitchOn = false
 
  override func viewDidLoad() {
    super.viewDidLoad()

    valueSwitch.on = isSwitchOn
  }
}

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

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

alt tag

uialertcontroller-customization's People

Contributors

maximbilan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

uialertcontroller-customization's Issues

iPad Air get Error

Hello
In iPad Air get Error, can you help me to solve it?

Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x7ff57f833a00>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a source view and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.