Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView addSubview in iOS7

Adding some controls to UIAlertView was deprecated in iOS7 using addSubview method. As I know Apple promised to add contentView property.

iOS 7 is released now and I see that this property is not added. That is why I search for some custom solution with ability to add progress bar to this alertView. Something for example similar to TSAlertView, but more ready for using in iOS 7.

like image 798
B.S. Avatar asked Sep 10 '13 21:09

B.S.


4 Answers

Here is a project on Github to add any UIView to an UIAlertView-looking dialog on iOS7.

(Copied from this StackOverflow thread.)

Custom iOS7 AlertView dialog

like image 114
Wimagguc Avatar answered Nov 17 '22 10:11

Wimagguc


It took me only 1 day to create my own alert view that looks exactly like Apple's

  1. Take a screenshot of Apple's alert for reference (font sizes, spacings, width)
  2. 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).
  3. Implement all the methods from UIAlertView on your custom alert.

  4. 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).

  5. Blurred background - use Apple's sample code (I used opaque white)
  6. 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
                     }];
like image 21
Sulthan Avatar answered Nov 17 '22 09:11

Sulthan


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.

image
(source: github.io)

like image 32
Scott Berrevoets Avatar answered Nov 17 '22 08:11

Scott Berrevoets


For those who love simple and effective methods with out having to write lines of code. Here is a cool solution without using any other private frame works for adding subviews to ios 7 alert views,i.e.

[alertView setValue:imageView forKey:@"accessoryView"];

Sample code for better understanding,

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(180, 10, 85, 50)];
UIImage *wonImage = [UIImage imageNamed:@"image.png"];
[imageView setImage:wonImage];

//check if os version is 7 or above
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
      [alertView setValue:imageView forKey:@"accessoryView"];
}else{
      [alertView addSubview:imageView];
}

Hope it helps some one,thanks :)

like image 27
Eshwar Chaitanya Avatar answered Nov 17 '22 09:11

Eshwar Chaitanya