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

最近的工程有一个个人资料页面,需要填写生日和地区的信息,需要自己定义个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(0, ViewHeight - customViewHeight, 320, customViewHeight)];
    }
    return self;
}

//重写layoutSubviews
-(void)layoutSubviews
{
    [super layoutSubviews];
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, ViewHeight - customViewHeight -NavBarHeight, 320, 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:0 animated:YES];
    [self.delegate actionSheet:self clickedButtonAtIndex:0];
}

- (void) docancel{
    [self dismissWithClickedButtonIndex:1 animated:YES];
    [self.delegate actionSheet:self clickedButtonAtIndex:1];
}

@end

猛戳下载原代码

原文地址:https://www.cnblogs.com/ymonke/p/3454672.html