自定义UIActionSheet

UIActionSheet类系IOS开发中实现警告框的重要的类,而在好多应用中,都对它进行了扩展,今天介绍一下自定义风格的UIActionSheet

一、自定义CustomActionSheet类

CustomActionSheet类继承UIActionSheet,具体的实现如下所示:

1)CustomActionSheet.h头文件

#import <Foundation/Foundation.h>

@interface CustomActionSheet : UIActionSheet {

UIToolbar* toolBar;

UIView* view;

}

@property(nonatomic,retain)UIView* view;

@property(nonatomic,retain)UIToolbar* toolBar;

/*因为是通过给ActionSheet 加 Button来改变ActionSheet, 所以大小要与actionsheet的button数有关

 *height = 84, 134, 184, 234, 284, 334, 384, 434, 484

 *如果要用self.view = anotherview.  那么another的大小也必须与view的大小一样

 */

-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title;

@end

   

2)CustomActionSheet.m实现文件

#import "CustomActionSheet.h"

@implementation CustomActionSheet

@synthesize view;

@synthesize toolBar;

-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title

{

       self = [super init];

           if (self) {

       int theight = height - 40;

       int btnnum = theight/50;

       for(int i=0; i<btnnum; i++){

        [self addButtonWithTitle:@" "];

         }

       toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

       toolBar.barStyle = UIBarStyleBlackOpaque;

       UIBarButtonItem *titleButton = [[UIBarButtonItem alloc] initWithTitle:title 

                                                                            style:UIBarButtonItemStylePlain 

                                                                            target:nil 

                                                                            action:nil];

        

       UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 

                                                                            style:UIBarButtonItemStyleDone 

                                                                            target:self

                                                                             action:@selector(done)];

        

      UIBarButtonItem *leftButton  = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" 

                                                                           style:UIBarButtonItemStyleBordered 

                                                                           target:self 

                                                                           action:@selector(docancel)];

        

       UIBarButtonItem *fixedButton  = [[UIBarButtonItem alloc]        initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 

                                                                                      target:nil 

                                                                                      action:nil];

        

      NSArray *array = [[NSArray alloc] initWithObjects:leftButton,fixedButton,titleButton,fixedButton,rightButton,nil];

      [toolBar setItems: array];

      [titleButton release];

      [leftButton  release];

      [rightButton release];

      [fixedButton release];

      [array       release];

      [self addSubview:toolBar];

      view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, height-44)];

      view.backgroundColor = [UIColor groupTableViewBackgroundColor];

      [self addSubview:view];

          }

    return self;

}

-(void)done
{
   [self dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)docancel
{
   [self dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)dealloc
{
   [view release];
   [super dealloc];

}

@end

二、利用自定义的CustomActionSheet类显示提示框

#import "TestActionSheetViewController.h"

#import "CustomActionSheet.h"

@implementation TestActionSheetViewController


-(IBAction)btndown

{

       CustomActionSheet* sheet = [[CustomActionSheet alloc] initWithHeight:284.0f 

                                                          WithSheetTitle:@"自定义ActionSheet"];

       UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,50, 320, 50)];

       label.text = @"这里是要自定义放的控制";

label.backgroundColor = [UIColor clearColor];

label.textAlignment = UITextAlignmentCenter;

[sheet.view addSubview:label];

[sheet showInView:self.view];

[sheet release];

}

@end


这里的UILabel是作一个示例,在这个位置你可以换成你自己的内容即可;

三、效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值