iPhone : How to set BackgroundColor of UIButton with buttonType UIButtonTypeCustom

前端 未结 15 1319
有刺的猬
有刺的猬 2020-12-01 13:45

i want to change the button color when it is clicked. I used :

[button setBackgroundColor:[UIColor redColor]];

but this shows red color onl

相关标签:
15条回答
  • 2020-12-01 14:42

    To get a button with a single color you can set the button to custom button. Here is my known list of available button api (you can set them in viewDidLoad after setting them as outlets). Just import quartz and change the border radius to make it look like a button.

    //highlightcolor - this changes the status for UIControlStateHighlighted
    OUTLETNAME.tintColor =  [UIColor orangeColor];
    
    //borderradius *quartz
    [[OUTLETNAME layer] setCornerRadius:20.0f];
    
    //border *quartz
    [[OUTLETNAME layer] setBorderWidth:5.0f];
    
    //bordercolor
    [OUTLETNAME.layer setBorderColor:[[UIColor greenColor] CGColor]];
    
    //backgroundcolor 
    OUTLETNAME.backgroundColor = [UIColor blackColor];
    
    //image 
    [OUTLETNAME setImage:[UIImage imageNamed:PICNAME] forState:UIControlStateNormal];
    
    //text 
    [OUTLETNAME setTitle:@"TEXT" forState:UIControlStateNormal];
    
    //fontsize
    OUTLETNAME.titleLabel.font = [UIFont systemFontOfSize:36.0];
    
    //fontcolor
    OUTLETNAME.titleLabel.textColor = [UIColor blackColor];
    
    //fontcolor
    [OUTLETNAME setTitleColor:[UIColor orangeColor] forState: UIControlStateNormal];
    

    If you want custom buttons with a gradient color you have to make a custom button class.

    This blog has a glossy button class project for download. http://didikot.com/?p=217

    You have to refractor and convert to ARC to use it, I think.

    This blog does the same thing but without the gloss. http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/

    0 讨论(0)
  • 2020-12-01 14:43

    You have everything correct. Just set your button type to UIButtonTypeCustom..

    button =    [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame    =   CGRectMake(200, 8, 100, 30);
    [button setTitle:@"Set up" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor greenColor]];
    button.clipsToBounds    =   YES;
    button.layer.cornerRadius   =   10;
    button.alpha        =   0.5;
    
    0 讨论(0)
  • 2020-12-01 14:44
    1. In .xib - set Button Type: Custom.
    2. Set Background Color as per you want.
    0 讨论(0)
提交回复
热议问题