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就完成了。