我想创建一个图像,标题和描述类似于UITableViewCellStyleSubtitle的按钮。我想以编程方式做到这一点。如何在iOS中创建像UITableViewCellStyleSubtitle这样的非常自定义的uibutton
有谁知道我该如何做到这一点?
我想创建一个图像,标题和描述类似于UITableViewCellStyleSubtitle的按钮。我想以编程方式做到这一点。如何在iOS中创建像UITableViewCellStyleSubtitle这样的非常自定义的uibutton
有谁知道我该如何做到这一点?
您可以创建一个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];
}
在上面代码,您可以根据需要调整帧。一旦设置完成,只需在视图/视图控制器中像正常一样创建按钮并调用上述方法即可:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 300, 200);
[myButton setupButton:myTitle image:myImage description:myDesc];
子类UIControl并覆盖drawRect
和/或添加子视图以创建所需的外观。