Friday, August 16, 2013

Replacement AlertView and ActionSheet

Give that iOS 7 has totally hosed any hope for custom AlertViews I hacked up a couple of nice options available on github.

Here's the finished product in a sample project.

DISCLAIMER: THIS SUITS MY NEEDS, if it doesn't suit your needs, sorry, look elsewhere...


LICENSE: matches the github MIT EXCEPT for Alex1987 - go use your fancy frackin' TSAlertView, 'mm-kay! ;)

DOWNLOAD LINK

:-)

Example of what you do by subclassing this:


This was accomplished just like how you'd do it pre-iOS 7 (code snip it below)

- (id)initWithTitle:(NSString *)title yoffset:(int)yoffset minValue:(int)minValue setValue:(int)setValue maxValue:(int)maxValue message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle image:(NSString*)imageName {

self = [super init];
if (!(self = [self initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okButtonTitle, nil])) return nil;
    CGFloat xCenter = kTTMaxContentFrameWidth * 0.5f;
    
    self.mySlider = [[UISlider alloc] initWithFrame:CGRectMake(xCenter-110, kSliderOffsetForY, 220, (kIsAniPad ? 48.0 : 36.0))];
    self.mySlider.minimumValue = (float)minValue;
    self.mySlider.maximumValue = (float)maxValue;
    self.mySlider.tag = 0;
    if (yoffset == -1) {
        self.mySlider.value = 0;
    }
    else {
        self.mySlider.value = (float)setValue;
    }
    
    self.mySlider.continuous = YES;
    [self.mySlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    
    // Setup custom slider images
    UIImage *minImage = [UIImage imageNamed:@"leftSide.png"];
    UIImage *maxImage = [UIImage imageNamed:@"rightSide.png"];
    UIImage *thumbImage = [UIImage imageNamed:@"thumb.png"];
    UIImage *thumbPressedImage = [UIImage imageNamed:@"thumbpressed.png"];
    
    minImage=[minImage stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
    maxImage=[maxImage stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
    
    // Setup the sliders
    [self.mySlider setMinimumTrackImage:minImage forState:UIControlStateNormal];
    [self.mySlider setMaximumTrackImage:maxImage forState:UIControlStateNormal];
    [self.mySlider setThumbImage:thumbImage forState:UIControlStateNormal];
    [self.mySlider setThumbImage:thumbPressedImage forState:UIControlStateHighlighted];
    [self.containerView addSubview: self.mySlider];

    if (minValue == maxValue) {
        self.mySlider.hidden = TRUE;
}
    
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(xCenter-120, kLabelOffsetForY, 240, 22)]; //20
self.myLabel.backgroundColor = [UIColor clearColor];
self.myLabel.textColor = [UIColor blueColor];
self.myLabel.font = kTTAlertSliderFont;
self.myLabel.textAlignment = UITextAlignmentCenter;
self.myLabel.text = [NSString stringWithFormat:@"Selected Amount: %i", (int)self.mySlider.value];

if (yoffset == 23) {
self.myLabel.text = [NSString stringWithFormat:@"Selected Amount: %i", (int)self.mySlider.value];
}
else {
self.myLabel.text = [NSString stringWithFormat:@"Selected Amount: %i", setValue];
}
[self.containerView addSubview:self.myLabel];
NSString *imagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"Alert/%@", imageName]];
    CGRect iframe = CGRectMake(8.0, 12.0+yoffset, 156.0, 60.0);
    if (kIsAniPad)
        iframe = CGRectMake(12.0, 14.0+yoffset, 312.0, 120.0);
    
self.myImageView = [[UIImageView alloc] initWithFrame:iframe];
self.myImageView.image = [UIImage imageWithContentsOfFile:imagePath];
    
[self.containerView addSubview:self.myImageView];    
    
return self;
}

- (void)alertView:(TTAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
pressedButton = buttonIndex;
}

2 comments:

  1. Be nice if allowed you to embed pickers and buttons :)

    ReplyDelete
  2. I'm stymied by your reply. I'll add a screenshot of one of the alerts I use in WARP gce, hopefully that will shed some light on its power.
    :-)

    ReplyDelete