UIAlertView addSubview in iOS7
Here is a project on Github to add any UIView to an UIAlertView-looking dialog on iOS7.
(Copied from this StackOverflow thread.)
It took me only 1 day to create my own alert view that looks exactly like Apple's
- Take a screenshot of Apple's alert for reference (font sizes, spacings, width)
- Create a xib with title, message, custom view and tables for buttons (Apple uses tables instead of
UIButton
now, default table cell is good enough). Note you need 3 button tables: two for left and right buttons (whenever the number of buttons is 2), another one for the other cases (one button or more than 2 buttons). Implement all the methods from
UIAlertView
on your custom alert.Show/Dismiss - you can create a specific modal window for your alerts but I just put my alerts on top of my root view controller. Register your visible alerts to a static array. If showing the first alert/dismissing the last, change tint mode of your window/view controller to dimmed/to automatic and add/remove a dimming view (black with alpha = 0.2).
- Blurred background - use Apple's sample code (I used opaque white)
- 3D dynamic effects - use Apple's sample code (5 lines of code). If you want a nice effect, take a slightly bigger snapshot in step 5 and add inverse animators for alert background and foreground.
EDIT:
Both blurred background and the paralax effect sample code can be found in "iOS_RunningWithASnap" WWDC 2013 sample code
Paralax effect:
UIInterpolatingMotionEffect* xAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis] autorelease];xAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];xAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];UIInterpolatingMotionEffect* yAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis] autorelease];yAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];yAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];UIMotionEffectGroup *group = [[[UIMotionEffectGroup alloc] init] autorelease];group.motionEffects = @[xAxis, yAxis];[self addMotionEffect:group];
The blurred background is the only complicated thing. If you can use an opaque color instead, use it. Otherwise it's a lot of experimenting. Also note that blurred background is not a good solution when the background is dark.
For the show/dismiss animationg, I am using the new spring animation method:
void (^animations)() = ^{ self.alpha = 1.0f; self.transform = CGAffineTransformIdentity;};self.alpha = 0.0f;self.transform = CGAffineTransformMakeScale(0.5f, 0.5f);[UIView animateWithDuration:0.3 delay:0.0 usingSpringWithDamping:0.7f initialSpringVelocity:0.0f options:UIViewAnimationOptionCurveLinear animations:animations completion:^(BOOL completed) { //calling UIAlertViewDelegate method }];
I wrote a full implementation of UIAlertView that mimics the complete UIAlertView API, but adds the contentView property we've all wanted for so long: SDCAlertView.
(source: github.io)