Change the state of a customized button (button with an image)?

前端 未结 6 2262
执笔经年
执笔经年 2021-02-11 09:34

I know how to change the state of the regular button, as well as change the background color of a regular button. However, when I do UIButton:setBackgroundImage (cu

6条回答
  •  孤城傲影
    2021-02-11 10:33

    To darken the button, assuming the background contains the image, you can add a sublayer to the view (button). The sublayer will overlay the background image. You use the layer to provide a "tint" over the background image.

    You can create a darkening layer with a black or grey layer that has an alpha of 0.3 or so. You can create a lightening layer with a white layer and an appropriate alpha.

    See the CALayer Class Reference to learn how to create a layer, then insert the sublayer into the button's view like this:

    [self.layer insertSublayer:darkeningLayer atIndex:0];
    

    I've used this to insert gradient layers over the background of a view.

提交回复
热议问题