The iOS button also enables rounded and shaded effects

Source: Internet
Author: User

Let's take a look at the rounded corners of a button, as shown in the following code:

Button.layer.masksToBounds =yes;

Button.layer.cornerRadius = 10;

To implement a button's shadow effect, the following code:

Button.shadowoffset = Cgsizemake (10, 10);

button.shadowopacity = 0.7;


At this point, if you want to realize the fillet and shadow effect, then there will be a problem, you set the maskstobounds to Yes at this point is rounded, but the shadow will be gone, you set it to No, the fillet is gone, the shadow has, so I began to full network to check, have not found a solution, Finally, in a forum, a brother said: need to use a layer to achieve the shadow, and then I follow this line of thought, wrote the following code

Calayer *layer = [Calayerlayer];

Layer.frame = CGRectMake (in the same position as the button and the same size);

Layer.backgroundcolor = [Uicolorblackcolor]. Cgcolor;

Layer.shadowoffset = Cgsizemake (10, 10);

layer.shadowopacity = 0.7;

Layer.cornerradius = 10;

Here, self represents the currently customized view

[Self.layeraddSublayer:layer];

UIButton *button = [[Uibuttonalloc]initwithframe:cgrectmake (a certain size of position)];

[Self Addsubview:button];

Button.layer.masksToBounds =yes;

Button.layer.cornerRadius = 10;

Results: At the same time to achieve the fillet and shadow, the idea is to add a layer between the button and the view, to achieve a fillet with a shadow, so that the final effect can be achieved, note must first upper button, so that the button on the layer.

Reprinted from: http://blog.csdn.net/jnbbwyth/article/details/50899611

The iOS button also enables rounded and shaded effects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.