Original address: http://blog.csdn.net/sjzsp/article/details/6688950
Because the UI has a special purpose, uiactionsheet and Uialertview do not allow you to customize the interface by addsubview such functions. The solution is to inherit it, implement a custom class, overload the Layoutsubviews function//. h
#import < uikit/uikit.h >
@interface Uiimageactionsheet:uiactionsheet {
UIImage * titleimage;
}
-(ID) initwithimage: (UIImage *) image
Title: (NSString *) title
Delegate:(ID < uiactionsheetdelegate >) delegate
Cancelbuttontitle: (NSString *) cancelbuttontitle
Destructivebuttontitle: (NSString *) destructivebuttontitle
Otherbuttontitles: (NSString *) otherbuttontitles;
@end
. m file
#import "UIImageActionSheet.h"
@implementation Uiimageactionsheet
-(ID) initwithimage: (UIImage *) image
Title: (NSString *) title
Delegate:(ID < uiactionsheetdelegate >) delegate
Cancelbuttontitle: (NSString *) cancelbuttontitle
Destructivebuttontitle: (NSString *) destructivebuttontitle
Otherbuttontitles: (NSString *) otherbuttontitles{
self = [Super Initwithtitle:title delegate:delegate
Cancelbuttontitle:cancelbuttontitle
Destructivebuttontitle:destructivebuttontitle
Otherbuttontitles:otherbuttontitles,nil];
if (self) {
Titleimage = image;
[Titleimage retain];
Uiimageview * ImageView = [[Uiimageview alloc] initwithimage:titleimage];
Imageview.frame = Cgrectzero;
//
For (UIView * Subview in Self.subviews) {
if (! [Subview Iskindofclass:[uilabel class]] {
[Self Insertsubview:imageview abovesubview:subview];
break;
}
}
[ImageView release];
}
return self;
}
-(CGFloat) maxlabelycoordinate {
Determine maximum y-coordinate of labels
CGFloat Maxy = 0;
for (UIView * view in Self.subviews) {
if ([View Iskindofclass:[uilabel class]]) {
CGRect viewframe = [view frame];
CGFloat LowerY = viewframe.origin.y + viewFrame.size.height;
if (LowerY > Maxy)
Maxy = LowerY;
}
}
return Maxy;
}
-(void) layoutsubviews{
[Super Layoutsubviews];
CGRect frame = [self frame];
CGFloat labelmaxy = [self maxlabelycoordinate];
for (UIView * view in Self.subviews) {
if (! [View Iskindofclass:[uilabel class]] {
if ([View Iskindofclass:[uiimageview class]]) {
CGRect viewframe = CGRectMake ((320-titleimage.size.width)/2, Labelmaxy + 10,
TitleImage.size.width, TitleImage.size.height);
[View Setframe:viewframe];
}
else if (! [View Iskindofclass:[uiimageview class]] {
CGRect viewframe = [view frame];
VIEWFRAME.ORIGIN.Y + = TitleImage.size.height + 10;
[View Setframe:viewframe];
}
}
}
FRAME.ORIGIN.Y-= titleImage.size.height + 2.0;
Frame.size.height + = titleImage.size.height + 2.0;
[Self setframe:frame];
}
/*
Only override drawrect:if you perform custom drawing.
An empty implementation adversely affects performance during.
-(void) DrawRect: (cgrect) Rect {
Drawing code.
}
*/
-(void) Dealloc {
[Super Dealloc];
if (titleimage) {
[Titleimage release];
}
}
@end
Use:
UIImage *tips3img = [uiimage imagenamed:@ "Tips-3.png"]; Uiimageactionsheet * Tipsactionsheet = [[Uiimageactionsheet alloc]
Initwithimage:tips3img
The pattern added by title: @ "can be adjusted in the following ways"
Delegate:self cancelbuttontitle: @ "Know"
Destructivebuttontitle:nil
Otherbuttontitles:nil];
Tipsactionsheet.tag = kactionsheettagtips;
[Tipsactionsheet ShowInView:self.view];
[Tipsactionsheet release];