我正在尝试使用本教程实现自定义警报视图。这篇文章中的所有内容都运行良好,但它并没有谈到自定义 UIAlertButton。然后,我尝试通过 layoutSubviews 方法对其进行自定义:
- (void)layoutSubviews
{
for (UIView *subview in self.subviews){ //Fast Enumeration
if ([subview isMemberOfClass:[UIImageView class]]) {
subview.hidden = YES; //Hide UIImageView Containing Blue Background
}
if ([subview isMemberOfClass:[UILabel class]]) { //Point to UILabels To Change Text
UILabel *label = (UILabel*)subview; //Cast From UIView to UILabel
label.textColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0f];
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0.0f, 1.0f);
}
if ([subview isMemberOfClass:[UIAlertButton class]]) {
// do blablabla
}
}
}
特别是我添加的是:
if ([subview isMemberOfClass:[UIAlertButton class]]) {
// do blablabla
}
但是找不到 UIAlertButton 类,为什么?谢谢
编辑:这是我的最终工作代码,感谢 nsguliver。
CustomAlertView.h
#import <UIKit/UIKit.h>
@interface CustomAlertView: UIAlertView {
NSMutableArray *fakeButtonIndexList;
NSMutableArray *buttonList;
}
-(void)addCustomButton:(NSString*)title;
@end
CustomAlertView.m
#import "CustomAlertView.h"
@implementation CustomAlertView
-(id) initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
if (self) {
buttonList = [NSMutableArray array];
fakeButtonIndexList = [NSMutableArray array];
}
return self;
}
-(void)layoutSubviews {
for (UIView *subview in self.subviews){ //Fast Enumeration
// Image de fond
if ([subview isMemberOfClass:[UIImageView class]]) {
subview.hidden = YES; //Hide UIImageView Containing Blue Background
}
// Label
if ([subview isMemberOfClass:[UILabel class]]) { //Point to UILabels To Change Text
UILabel *label = (UILabel*)subview; //Cast From UIView to UILabel
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0.0f, 1.0f);
}
}
}
-(void)drawRect:(CGRect)rect {
//////////////GET REFERENCE TO CURRENT GRAPHICS CONTEXT
CGContextRef context = UIGraphicsGetCurrentContext();
//////////////CREATE BASE SHAPE WITH ROUNDED CORNERS FROM BOUNDS
CGRect activeBounds = self.bounds;
CGFloat cornerRadius = 10.0f;
CGFloat inset = 6.5f;
CGFloat originX = activeBounds.origin.x + inset;
CGFloat originY = activeBounds.origin.y + inset;
CGFloat width = activeBounds.size.width - (inset*2.0f);
CGFloat height = activeBounds.size.height - (inset*2.0f);
CGRect bPathFrame = CGRectMake(originX, originY, width, height);
CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:bPathFrame cornerRadius:cornerRadius].CGPath;
//////////////CREATE BASE SHAPE WITH FILL AND SHADOW
CGContextAddPath(context, path);
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:40.0f/255.0f green:40.0f/255.0f blue:40.0f/255.0f alpha:1.0f].CGColor);
CGContextDrawPath(context, kCGPathFill);
//////////////CLIP STATE
CGContextSaveGState(context); //Save Context State Before Clipping To "path"
CGContextAddPath(context, path);
CGContextClip(context);
}
-(void)show {
[super show];
// On redéfinit les frames des boutons
NSInteger indexCustomButton = 0;
for (NSString *indexStr in fakeButtonIndexList) {
UIButton *customButton = (UIButton*)[buttonList objectAtIndex:indexCustomButton];
UIButton *fakeButton = (UIButton*)[self.subviews objectAtIndex:[indexStr integerValue]];
[customButton setFrame:fakeButton.frame];
indexCustomButton++;
}
}
-(void)addCustomButton:(NSString *)title {
[self addButtonWithTitle:title];
UIButton *fakeButton = [self.subviews lastObject];
[fakeButton setHidden:YES];
[fakeButtonIndexList addObject:[NSString stringWithFormat:@"%d",[self.subviews count]]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:fakeButton.frame];
[button setTitle:title forState:nil];
[button setBackgroundColor:[Templates getColor:@"color"]];
[button.layer setCornerRadius:5.0f];
[buttonList addObject:button];
[self addSubview:button];
}
@end