Coloration of UIButtons of type UIButtonTypeRoundedRect

后端 未结 4 1296
闹比i
闹比i 2021-01-19 14:38

I would like the rounded rectangle portion of the subject type of UIButton to be a solid custom color I specify. I understand that

. setTitleColor : changes the tex

相关标签:
4条回答
  • 2021-01-19 15:14

    Try

    CALayer *subLayer = [CALayer layer];
    subLayer.frame = self.theButton.bounds;
    subLayer.cornerRadius = 10.0;
    subLayer.opacity = 1.0;
    
    CALayer *imageLayer = [CALayer layer];
    imageLayer.frame = subLayer.bounds;
    imageLayer.cornerRadius = 10.0;
    
    
    imageLayer.masksToBounds = YES;
    imageLayer.opacity = 1.0;
    imageLayer.contents = (id) [UIImage imageNamed:@"your.png"].CGImage;
    
    [subLayer addSublayer:imageLayer];
    
    [self.theButton.layer addSublayer:subLayer];
    
    0 讨论(0)
  • 2021-01-19 15:17

    You can try to grab the layer and set its corner radius in a custom button. I haven't tried it, but it seems worth a try.

    [[button layer] setCornerRadius:12.0f];
    [[button layer] setMasksToBounds:YES];
    [[button layer] setBackgroundColor:[[UIColor redColor] CGColor]];
    

    You'll have to add the QuartzCore framework to your project (and #import ) in order to take advantage of these methods.

    Just an addition, in this case you use button type as "Custom". So the button can be declared as : UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

    0 讨论(0)
  • 2021-01-19 15:19

    Does using backgroundColor of UIButton's titleLabel work?

    0 讨论(0)
  • 2021-01-19 15:32

    I think that the easiest way to do this would just be to create a solid rounded button png image of your own, with transparencies around the rounded corners.

    Then just set the background of your custom button to be that image that you created.

    Other than that, I don't actually think that this is can be done...Not sure...

    0 讨论(0)
提交回复
热议问题