【问题标题】:How to generate ColorfulButtons Dynamically?如何动态生成彩色按钮?
【发布时间】:2012-07-02 09:30:52
【问题描述】:

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

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

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

【问题讨论】:

    标签: iphone objective-c xcode4.2


    【解决方案1】:
     [phoneButton setTitleColor:(UIColor *) forState:(UIControlState)];   
      [phoneButton setBackgroundColor:(UIColor *)];   
      [phoneButton setBackgroundImage:(UIImage *) forState:(UIControlState)];  
    

    【讨论】:

    • 没有@ganesh manoj。我不想在按钮上设置图像。在上面的链接中。如果我使用`IBOutlet ColorfulButton *phBtn;` 然后应用[phoneButton setHighColor:[UIColor redColor]]; [phoneButton setLowColor:[UIColor orangeColor]]; 它确实有效。但它不适用于动态按钮。正如我在上面的评论中提到的那样。
    • 这一行[phoneButton setBackgroundColor:(UIColor *)]; 将设置整个按钮的背景颜色。虽然我使用的代码在按钮上设置了两种颜色。 setHighColor 设置上半部分颜色。 setLowColor 设置按钮的下半部分颜色。
    【解决方案2】:

    这也让我困惑了很长时间,但只是想通了,所以我想我会发布我的解决方案。 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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-17
      • 2015-11-21
      • 2023-03-16
      • 2013-01-02
      • 2011-05-13
      • 2019-04-22
      • 2022-01-02
      • 1970-01-01
      相关资源
      最近更新 更多