【ios开发】自定义Actionsheet实现时间选择器和省市区选择器

时间:2023-03-09 17:48:46
【ios开发】自定义Actionsheet实现时间选择器和省市区选择器

最近的工程有一个个人资料页面,需要填写生日和地区的信息,需要自己定义个actionsheet。

但是到网上搜了一下都不太合适,只好自己研究研究,重写了一个。共享出来给大家用用,突然发现自己精神很高尚吗、,哈哈,其实是方便自己以后遇到类似的工程直接引用就好了。

先上效果图:

【ios开发】自定义Actionsheet实现时间选择器和省市区选择器【ios开发】自定义Actionsheet实现时间选择器和省市区选择器

结构图:

【ios开发】自定义Actionsheet实现时间选择器和省市区选择器

这里简单介绍一下Actionsheet类

.h

//
// CustomActionSheet.h
// test
//
// Created by wxian on 13-12-2.
// Copyright (c) 2013年 wxian. All rights reserved.
// #import <UIKit/UIKit.h> @interface CustomActionSheet : UIActionSheet <UIActionSheetDelegate>
{ NSString* customTitle;
} -(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title;
@property (nonatomic, retain) UIView *customView; @end

.m

//
// CustomActionSheet.m
// test
//
// Created by wxian on 13-12-2.
// Copyright (c) 2013年 wxian. All rights reserved.
// #import "CustomActionSheet.h" #define NavBarHeight 40
#define ViewHeight 459 @interface CustomActionSheet(){
@private
float customViewHeight;
} @end @implementation CustomActionSheet -(id)initWithHeight:(float)height WithSheetTitle:(NSString *)title
{
self = [super init];
if (self) {
customViewHeight = height;
customTitle = title;
self.customView = [[UIView alloc] initWithFrame:CGRectMake(, ViewHeight - customViewHeight, , customViewHeight)];
}
return self;
} //重写layoutSubviews
-(void)layoutSubviews
{
[super layoutSubviews];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(, ViewHeight - customViewHeight -NavBarHeight, , NavBarHeight)];
navBar.barStyle = UIBarStyleDefault;
UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:customTitle];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(docancel)];
navItem.leftBarButtonItem = leftButton;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleBordered target:self action:@selector(done)];
navItem.rightBarButtonItem = rightButton;
NSArray *array = [[NSArray alloc ]initWithObjects: navItem, nil];
[navBar setItems:array];
[self.superview addSubview:navBar];
[self.superview addSubview:self.customView]; } - (void) done{
[self dismissWithClickedButtonIndex: animated:YES];
[self.delegate actionSheet:self clickedButtonAtIndex:];
} - (void) docancel{
[self dismissWithClickedButtonIndex: animated:YES];
[self.delegate actionSheet:self clickedButtonAtIndex:];
} @end

猛戳下载原代码