使用图像创建自定义UIButton

3

我正在尝试设置单元格的附属视图,它是一个UIButton。但是它没有显示出来。

以下是代码:

UIButton *accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"plus.png"] forState:UIControlStateNormal];
[cell setAccessoryView:accessory];

尝试使用[cell.contentView accessory];根据需要调整附件的位置。 - rptwsthi
3个回答

6

调用 [accessory sizeToFit]; 根据图片来调整其尺寸。


0

你需要像这样为这个附件设置一个框架

UIButton *accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"plus.png"] forState:UIControlStateNormal];
//Set the accessory frame
accessory.frame = CGRectMake(0, 0, 100, 100);

0
UIView* accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 50)];
UIImageView* accessoryViewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
accessoryViewImage.center = CGPointMake(12, 25);
[accessoryView addSubview:accessoryViewImage];
[cell setAccessoryView:accessoryView];
[accessoryViewImage release];
[accessoryView release];

就这样做。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接