嗨我有一个由UIButtons组成的弹出式菜单,我需要在菜单的每个按钮中显示图像。我需要为每个单独的按钮显示不同的图像。共有6个按钮。
我该怎么做
- (NSInteger)makeOneButton:(NSString *)title action:(SEL)action top:(NSNumber *)top into:(UIView *)parent
{
// NOTE: if you want more stuff here, I'd put all of the items in a view (including this button)
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:title forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:@"paper.png"] forState:UIControlStateNormal];
//UIButtonTypeCustom
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:7]];
[parent addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(top);
make.left.equalTo(parent.mas_left);
make.width.equalTo(parent);
make.height.equalTo([NSNumber numberWithInteger:mCurrentCellHeight]);
}];
return mCurrentCellHeight;
}
答案 0 :(得分:0)
在此方法中添加另一个参数,即图像名称或图像,并添加为按钮的背景,当您调用此方法添加按钮时,也告诉方法您想要设置相应按钮的图像。像这样....
- (NSInteger)makeOneButton:(NSString *)title action:(SEL)action top:(NSNumber *)top imageName:(NSString *)imageName into:(UIView *)parent
{
// NOTE: if you want more stuff here, I'd put all of the items in a view (including this button)
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:title forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:@"paper.png"] forState:UIControlStateNormal];
//UIButtonTypeCustom
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:7]];
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[parent addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(top);
make.left.equalTo(parent.mas_left);
make.width.equalTo(parent);
make.height.equalTo([NSNumber numberWithInteger:mCurrentCellHeight]);
}];
return mCurrentCellHeight;
}