我使用以下链接将颜色应用于按钮:
http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/
我应该如何使用上面的动态按钮? 我的代码是:
int y=297;
for (int i=0; i < [phonesArray count]; i++) {
NSLog(@"%d phone number is :%@",i,[phonesArray objectAtIndex:i]);
UIButton *phoneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[phoneButton setTitle:[phonesArray objectAtIndex:i] forState:UIControlStateNormal];
[phoneButton addTarget:self action:@selector(dailPhoneNo:) forControlEvents:UIControlEventTouchUpInside];
phoneButton.frame = CGRectMake( 11, y, 278, 42);
[scrollView addSubview:phoneButton];
y=y+60;
}
我也尝过这个:
ColorfulButton *phoneButton = [[ColorfulButton alloc] init];
[phoneButton setTitle:[phonesArray objectAtIndex:i] forState:UIControlStateNormal];
[phoneButton addTarget:self action:@selector(dailPhoneNo:) forControlEvents:UIControlEventTouchUpInside];
phoneButton.frame = CGRectMake( 11, y, 278, 42);
[phoneButton setHighColor:[UIColor redColor]];
[phoneButton setLowColor:[UIColor orangeColor]];
[scrollView addSubview:phoneButton];
但按钮没有出现在视图上。
答案 0 :(得分:0)
[phoneButton setTitleColor:(UIColor *) forState:(UIControlState)];
[phoneButton setBackgroundColor:(UIColor *)];
[phoneButton setBackgroundImage:(UIImage *) forState:(UIControlState)];
答案 1 :(得分:0)
这让我长时间难以忍受,但只是想出来,所以我想发布我的解决方案。 CIMGF的ColorfulButton类非常酷,但以编程方式创建一个并不容易。
我在ColorfulButton类中添加了一个额外的方法来简化整个过程。
在ColorfulButton.h中- (ColorfulButton *)initWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag;
在ColorfulButton.m
- (void)configureColors
{
[self setHighColor:[UIColor whiteColor]];
[self setLowColor:[UIColor grayColor]];
}
- (ColorfulButton *)initWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag
{
self = [super init];
self.frame = frame;
[self setTitleColor:[UIColor colorWithRed:.196 green:0.3098 blue:0.52 alpha:1.0] forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]];
[self configureColors];
[self awakeFromNib];
[self setTitle:title forState:UIControlStateNormal];
self.tag = tag;
return self;
}
在我的.m程序中
ColorfulButton *button = [[ColorfulButton alloc] initWithFrame:CGRectMake(70.0, 50.0, 100.0, 37.0) title:@"Category" tag:888];
[button addTarget:self
action:@selector(setCategoriesPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];