如何在iOS中创build像UITableViewCellStyleSubtitle这样的非常自定义的uibutton

我想创build一个图像,标题和描述类似于UITableViewCellStyleSubtitle的button。 我想以编程方式做到这一点。

有谁知道我可以做到这一点?

你可以创build一个UIButton类的类。 这里有一个为您准备的准系统:

在.h文件中:

@interface UIButton (YourCategoryName) - (void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description; @end 

在.m文件中:

@implementation UIButton(YourCategoryName)

 - void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description { UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 200, 50)]]; [self addSubview:titleLabel]; [titleLabel release]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image]; [self addSubview:imageView]; [imageView release]; UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 200, 100)]; [self addSubview:descriptionLabel]; [descriptionLabel release]; } 

在上面的代码中,您可以根据需要调整帧。 一旦设置完成,只需在view / viewcontroller中像普通的那样创buildbutton,然后调用上面的方法:

  UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; myButton.frame = CGRectMake(0, 0, 300, 200); [myButton setupButton:myTitle image:myImage description:myDesc]; 

子类UIControl并覆盖drawRect和/或添加子视图来创build所需的外观。