I want to create a custom alert view
within my iOS application. For example, I want to put some images
in this alert
, and change its color.
I know how to create a normal UIAlertView
, but is there a way to customize an alert view
?
Enter Swift as Language and Storyboard as User Interface. Choose Next. Go to the Storyboard, drag a Button from the Object Library to the View Controller inside the Storyboard. Double-click the Button and give it a title of"Display Action Sheet".
To present a ViewController in a NavigationController you can wrap ViewController into a NavigationController as it is in the example below. Then present NavigationController which contains ViewController. Check out the below video courses to learn more about Mobile App Development for iOS platform with Swift.
I set up my own UIViewController which I can skin with my own images. I generally only use one or two buttons, so I hide the second button if it's not being used. The view is actually the size of the entire screen, so it blocks touches behind it, but it is mostly transparent, so the background shows through.
When bringing it in, I use a few animations to make it bounce like Apple's alert view. Something like this works:
-(void)initialDelayEnded { self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001); self.view.alpha = 1.0; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/1.5]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(bounce1AnimationStopped)]; self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1); [UIView commitAnimations]; } - (void)bounce1AnimationStopped { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(bounce2AnimationStopped)]; self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9); [UIView commitAnimations]; } - (void)bounce2AnimationStopped { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; self.view.transform = CGAffineTransformIdentity; [UIView commitAnimations]; }
I have the possibility of a short delay built into the class, so initialDelayEnded is called when that delay is over.
When initializing, I pass in an object and selector I want called when each button is pressed, and then I call the appropriate selector on the object when a button is pressed.
Here's a custom alert view that I wrote which is a drop-in replacement for UIAlertView. You can set a custom background image; it wouldn't be hard to extend to support custom background colors.
https://github.com/TomSwift/TSAlertView
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With