根据前辈的工作照样子写了一个自定义类,点击可以实现下拉功能。
在rootViewController的viewDidLoad方法中:
-
(void)viewDidLoad
{
[super viewDidLoad];
self.view = [[UIView
alloc] initWithFrame:[[UIScreen mainScreen]
bounds]];
DropDownList *tf=[[DropDownList
alloc]initWithFrame:CGRectMake(100, 70, 140,30)];
tf.borderStyle=UITextBorderStyleRoundedRect;
[self.view
addSubview:tf];
[tf release];
}
其中 DropDownList是自定义的下拉菜单类。
在
DropDownList.m中有两个方法:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//默认的下拉列表中的数据
list=[[NSArray
alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
borderStyle =
UITextBorderStyleBezel;
showList=NO; //默认不显示下拉框
oldFrame=frame; //未下拉时控件初始大小
//当下拉框显示时,计算出控件的大小。
newFrame=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width,
frame.size.height*5);
lineColor=[UIColor
lightGrayColor];//默认列表边框线为灰色
listBgColor=[UIColor
whiteColor];//默认列表框背景色为白色
lineWidth=1;
//默认列表边框粗细为1
//把背景色设置为透明色,否则会有一个黑色的边
self.backgroundColor=[UIColor
clearColor];
[self
drawView]; //调用方法,绘制控件
}
return self;
}
-
(void)drawView
{
//文本框
textField=[[UITextField alloc]
initWithFrame:CGRectMake(0, 0,oldFrame.size.width-30,
oldFrame.size.height)];
textField.borderStyle=borderStyle;//设置文本框的边框风格
[self addSubview:textField];
}