我可以向UIActionSheet添加自定义UI元素吗?

时间:2011-09-16 15:32:48

标签: objective-c ios cocoa-touch uiactionsheet

我已成功将Buttons添加到我的UIActionSheet中。我想知道我是否可以添加其他元素,调整不透明度,并使用我的UIActionSheet改变面板飞行的方向(默认来自下方)?

我对iOS编程比较陌生,所以非常感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

如果您要将其自定义到那个程度,那么最好只创建自己的自定义叠加层视图并添加所需的控件。特别是调整动画方向会比它的价值更麻烦。

有关自定义视图和视图层次结构的更多信息,以及有关动画的有用部分(请参阅侧栏),请查看View Programming Guide for iOS

答案 1 :(得分:2)

写实现!

//.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 animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
    [super dealloc];
    if (titleImage) {
        [titleImage release];
    }
}


@end

答案 2 :(得分:1)

您必须创建自定义叠加层