简体   繁体   English

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

[英]Can I add custom UI Elements to UIActionSheet?

I've successfully added Buttons to my UIActionSheet. 我已经成功将Buttons添加到我的UIActionSheet中。 I was wondering if I could add other elements, adjust the opacity, and alter the direction of the panel flying in (default comes in from below)with my UIActionSheet? 我想知道是否可以使用UIActionSheet添加其他元素,调整不透明度以及更改面板的飞行方向(默认情况是从下方输入)?

I am relatively new to iOS programming so any help would be greatly appreciated. 我是iOS编程的新手,所以将不胜感激。

If you're going to customize it to that degree, you're better off just creating your own custom overlay view and adding whatever controls you need to it. 如果要自定义此程度,最好只创建自己的自定义叠加视图并向其中添加所需的任何控件。 Adjusting the animation direction in particular would be more trouble than it's worth. 特别地,调整动画方向将比值得的麻烦更多。

For more information on custom views and the view hierarchy, plus a useful section on animations (see the sidebar), check out the View Programming Guide for iOS . 有关自定义视图和视图层次结构的更多信息,以及有关动画的有用部分(请参见侧栏),请查看《适用于iOS视图编程指南》

Write implemention! 写实现!

//.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

您必须创建一个自定义叠加层

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM