iOS幾行代碼讓界面變圓角
最近有一些比較流行的應用,界面都是圓角的。比如Memopad,Pinterest之類的,都是。
琢磨了一下,發現這個其實很簡單。我們只需要在UIView上做點功夫就可以了。
創建一個UIView的Category,名為 UIView+RoundCorner
,在頭文件 UIView+RoundCorner.h
中聲明如下:
#import <UIKit/UIKit.h> @interface UIView (RoundCorner) -(void)makeRoundedCorner:(CGFloat)cornerRadius; @end
在實現文件 UIView+RoundCorner.h
代碼如下:
#import "UIView+RoundCorner.h"@implementation UIView(RoundCorner)
-(void)makeRoundedCorner:(CGFloat)cornerRadius { CALayer *roundedlayer = [self layer]; [roundedlayer setMasksToBounds:YES]; [roundedlayer setCornerRadius:cornerRadius]; } @end</pre>
使用方法
讓某個UI元素變圓角
對所有UIView或者其派生類,直接使用該方法即可,比如下面的代碼讓一個UIButton變圓角了:
[btn makeRoundedCorner:12.0f];
療效如下(為了讓療效看起來更明顯,我把按鈕弄黑了):
Paste_Image.png
讓整個界面變圓角
只需要在ViewContolloer中調用這個方法就好了,比如:
[self.view makeRoundedCorner:12.0f];
如果你的ViewController在
UITabBarController
中,你只需要調用這個方法就可以讓整個界面所有的界面都變成圓角的。比如
if (self.parentViewController) { [self.parentViewController.view makeRoundedCorner:12.0f]; }
療效如下:
![]()
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!