The addSubview is not available in UIAlertView since iOS7. The view hierarchy for this class is private and must not be modified.
As a solution, this class creates an iOS-style dialog which you can extend with any UIViews or buttons. The animations and the looks are copied too and no images or other resources are needed.
Install
As simple as adding the following files to your project:
CustomIOSAlertView.h
CustomIOSAlertView.m
Or use Cocoapods:
pod 'CustomIOSAlertView', '~> 0.9.5'
Change notes
Fixed rotation for IOS8
Removed 7 from the class name. Just use CustomIOSAlertView from now on, like: [[CustomIOSAlertView alloc] init];
The initWithParentView method is now deprecated. Please use the init method instead, where you don't need to pass a parent view at all. In case the init doesn't work for you, please leave a note or open an issue here.
You can enable or disable the iOS7 parallax effects on the alert view
[alertView setUseMotionEffects:TRUE];
Handle button clicks with a custom delegate
First, set the delegate:
[alertView setDelegate:self];
Then add the delegate methods:
- (void)customIOS7dialogButtonTouchUpInside: (CustomIOSAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
NSLog(@"Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);
}
Handle button clicks with a code block
[alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
NSLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);
[alertView close];
}];
You can also disable all other delegates by:
[alertView setDelegate:self];
```
Todos
This is a really quick implementation, and there are a few things missing:
Adding more buttons: they don't exactly match the look with that of on iOS7
Rotation: rotates wrong with the keyboard on
Special thanks to
@tamasdancsi for his support with the initial code
@dingosky for his work on the parallax effects code
@raspu for his work on the protocol delegates, iOS6 support and onButtonClick blocks
@sbandol for his idea on adding the AlertView as the top most view in the hierarchy
请发表评论