Chinaunix首页 | 论坛 | 博客
  • 博客访问: 306124
  • 博文数量: 53
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 325
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-14 22:50
文章分类

全部博文(53)

文章存档

2014年(15)

2013年(38)

我的朋友

分类: iOS平台

2013-11-20 23:39:53

根据前辈的工作照样子写了一个自定义类,点击可以实现下拉功能。
在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];

}




阅读(2248) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~