0

我使用以下链接将颜色应用于按钮:

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]; 

但是按钮没有出现在视图上。

4

2 回答 2

0
 [phoneButton setTitleColor:(UIColor *) forState:(UIControlState)];   
  [phoneButton setBackgroundColor:(UIColor *)];   
  [phoneButton setBackgroundImage:(UIImage *) forState:(UIControlState)];  
于 2012-07-02T09:37:28.567 回答
0

这也让我困惑了很长时间,但只是想通了,所以我想我会发布我的解决方案。CIMGF 的 ColorfulButton 类非常酷,但是以编程方式创建一个并不容易。

我在 ColorfulButton 类中添加了一个额外的方法来简化整个过程。

在彩色按钮.h

- (ColorfulButton *)initWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag;

在彩色按钮.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];
于 2013-01-21T04:32:19.260 回答