IOS之CustomActionSheet

CustomActionSheet

在IOS开发中不可避免的要用到自定义的ActionSheet,今天整理了一下如何实现自定义的ActionSheet。

首先,创建一个基于UIActionSheet的类,命名为CustomActionSheet。

在这个类中声明两个对象,如下所示:

@property (nonatomic, strong) UIView* customView;
@property (nonatomic) CGFloat height;

既定义了一个自定义的View还有设置这个View高度的参数。

紧接着声明一个初始化方法:

- (id)initWithHeight:(CGFloat)height;

完成基本的声明我们就可以开始具体代码的编写了。

完成初始化方法:

- (id)initWithHeight:(CGFloat)height
{
    self = [super init];
    if (self) {
        // Initialization code
        _height = height;
        CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
        _customView = [[UIView alloc] initWithFrame:CGRectMake(0, screenHeight - _height, 320, _height)];
        _customView.backgroundColor = [UIColor darkGrayColor];
    }
    return self;
}

接下来就是代码的核心部分:

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.superview addSubview:self.customView];
}

这就可以把自定义的View添加到系统提供的UIActionSheet上了。

我们在使用这个类时跟使用系统的UIActionSheet一样即可。

- (IBAction)buttonClicked:(id)sender
{
    CustomActionSheet* customActionSheet = [[CustomActionSheet alloc] initWithHeight:216.0f withTitle:nil];
    
    customActionSheet.delegate = self;
    [customActionSheet showInView:self.view];
}

到此自定义ActionSheet就完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值