解决的办法是继承它,实现一个自定义类,重载layoutSubviews函数。
#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
#import "UIImageActionSheet.h"
@implementation UIImageActionSheet
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(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
{
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-30);
[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];
}
-(void)dealloc
{
[super dealloc];
if (titleImage)
{
[titleImage release];
}
}
@end
使用:
UIImage *img = [UIImage imageNamed:@"baidu_sylogo1.gif"];
UIImageActionSheet *imageActionSheet = [[UIImageActionSheet alloc] initWithImage:img
title:@"添加的图案可用以下方式"
delegate:self
cancelButtonTitle:@"OK"
destructiveButtonTitle:nil
otherButtonTitles:nil];
//在开发过程中,发现有时候UIActionSheet的最后一项点击失效,点最后一项的上半区域时有效,这是在特定情况下才会发生,这个场景是试用了UITabBar的时候才有。解决方法
[imageActionSheet showInView:self.view.window];
//让动态添加的按钮在取消按钮之上
//[imageActionSheet addButtonWithTitle:@"知道了"];
//[imageActionSheet addButtonWithTitle:@"OK"];
//[imageActionSheet addButtonWithTitle:NSLocalizedString(@"取消", @"cancel button title")];
//imageActionSheet.cancelButtonIndex = imageActionSheet.numberOfButtons - 1;
[imageActionSheet release];