IOS: Two ways to implement UIView chamfer

Source: Internet
Author: User

Reprinted from: HTTP://WWW.JIANSHU.COM/P/451B7FA94E2A

The first kind: I think you see the code, the moment there is the urge to vomit, the most common one way ...

 uibutton *button = [[UIButton Alloc]init]; Button.frame  = CGRectMake (100 , 100 , 100 , 40   = [Uicolor Redcolor]; Button.layer.cornerRadius  = 20.0f  ; Button.layer.masksToBounds  = YES; 
[Button Settitle: @ " test " Forstate:uicontrolstatenormal]; [Self.view Addsubview:button];

So simple ... However, it is by default forced to cut off the four corners ah ... That's the problem, if the demand only requires cutting a corner ... Look at the second method.

The second: or in layer the fuss, the different use of the extension of the method, followed by the UIButton example, the specific effect to see:

#import "Uibutton+corner.h"@implementationUIButton (Corner)- (void) corner{cgrect bounds=Self.bounds; Uibezierpath*maskpath = [Uibezierpath bezierpathwithroundedrect:bounds byroundingcorners:uirectcornerbottomleft cornerRadii: Cgsizemake ( -, -)]; Cashapelayer*masklayer =[Cashapelayer layer]; Masklayer.frame=bounds; Masklayer.path=Maskpath.cgpath;    [Self.layer Addsublayer:masklayer]; Self.layer.mask=Masklayer;}@end


typedef ns_options (Nsuinteger, Uirectcorner) {    uirectcornertopleft     10,    uirectcornertopright     1 1 ,    Uirectcornerbottomleft  12,    13,    uirectcornerallcorners   = ~0UL};

 

IOS: Two ways to implement UIView chamfer

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.