我想創建一個圖像,標題和描述類似於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
和/或添加子視圖以創建所需的外觀。