自定义UIButton与图像

时间:2012-06-22 07:02:23

标签: iphone ios uitableview uibutton

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

这是代码

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

3 个答案:

答案 0 :(得分:6)

调用[accessory sizeToFit];根据图片调整其大小。

答案 1 :(得分: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);

答案 2 :(得分: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];

就这样做。