UIAlertView addSubview in iOS7

2019-01-01 02:28发布

问题:

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.

回答1:

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

(Copied from this StackOverflow thread.)

\"Custom



回答2:

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
                     }];


回答3:

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\"



回答4:

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 :)



回答5:

For IOS7

UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@\"Enter Form Name\" 
                                               message:@\"\"
                                               delegate:self 
                                               cancelButtonTitle:@\"Cancel\"
                                               otherButtonTitles:@\"Ok\", nil];
alertView1.alertViewStyle = UIAlertViewStyleSecureTextInput;
UITextField *myTextField = [alertView1 textFieldAtIndex:0];
[alertView1 setTag:555];
myTextField.keyboardType=UIKeyboardTypeAlphabet;

[alertView1 show];


回答6:

There wont be UIAlertView with custom views in iOS7, nor contentView which Apple changed its mind about, so addSubview is impossible now in UIAlertView.

A good alternative will be SVProgressHUD, according to many threads in Apple\'s forum.

Edit:

There is officially no addSubview nor subclassing for UIAlertView in iOS7.

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

Other good alternatives:

ios-custom-alertview by wimagguc

MZFormSheetController.



回答7:

You can find simple solution without extra classes here

It is based on setting accessoryView for ordinary UIAlertView.



回答8:

PKAlertController (https://github.com/goodpatch/PKAlertController) is great library. I tested a lot of similar libraries and just this satisfied all my requirements.

Why it is cool:

  • Supports custom view
  • Supports iOS7
  • It is view controller
  • It behaves and looks like native alert view, including motion effects
  • Customizable
  • Similar interface like in UIAlertController