iOS開發之如何自訂UIActionSheet(UIAlertView)中的內容

來源:互聯網
上載者:User

原文地址:http://blog.csdn.net/sjzsp/article/details/6688950

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];



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.