• 如何自定义UIActionSheet(UIAlertView)中的内容


    UIActionSheet和UIAlertView因为UI有特殊的用途,所以本身并不允许你AddSubview之类的函数来自定义界面。解决的办法是继承它,实现一个自定义类,重载layoutSubviews函数

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

    使用: 

     UIImage *tips3Img = [UIImage imageNamed:@"tips-3.png"];

    UIImageActionSheet *tipsActionSheet = [[UIImageActionSheet alloc] 
                                           initWithImage:tips3Img 
                                           title:
    @"添加的图案可用以下方式调整" 
                                           
    delegate:self cancelButtonTitle:@"知道了" 
                                           destructiveButtonTitle:nil 
                                           otherButtonTitles:nil];
    tipsActionSheet.tag 
    = kActionSheetTagTips;
    [tipsActionSheet showInView:self.view];
    [tipsActionSheet release];

    效果:

  • 相关阅读:
    django路由——关于路由最后斜杠的问题
    django-admin 汉化
    动态绑定属性、方法 以及 __slots__
    type()、获取对象信息
    String,StringBuffer,StringBuilder三种的性能差异
    线程创建的三种方式的比较(继承Thread,Runnable接口,Callable接口)
    单例设计模式
    用Dockerfile来build原理和过程
    etcd和redis
    win10部署docker和mongo
  • 原文地址:https://www.cnblogs.com/think/p/CustomUIActionSheetView.html
Copyright © 2020-2023  润新知