Xcode
只要幾行代碼叫惊,就能夠給整個界面視圖添加圓角效果,也可以給單獨的控件添加圓角效果,接下來看效果吧审丘。
創(chuàng)建分類
-
首先創(chuàng)建一個
UIView
的分類,名為UIView+RoundCorner
勾给。
創(chuàng)建分類 在頭文件
UIView+RoundCorner.h
中聲明如下:
#import <UIKit/UIKit.h>
@interface UIView (RoundCorner)
/**
給當(dāng)前頁面添加圓角效果
@param cornerRadius 圓角半徑
*/
- (void)makeRoundedCorner:(CGFloat)cornerRadius;
@end
-
UIView+RoundCorner.m
代碼如下:
#import "UIView+RoundCorner.h"
@implementation UIView (RoundCorner)
- (void)makeRoundedCorner:(CGFloat)cornerRadius {
CALayer *roundedlayer = [self layer];
[roundedlayer setMasksToBounds:YES];
[roundedlayer setCornerRadius:cornerRadius];
}
@end
使用方法
在需要使用該方法的類中引入 #import "UIView+RoundCorner.h"
頭文件滩报。
1. 讓某個UI元素變圓角
對所有繼承自 UIView
的控件,直接使用該方法也可以生成圓角播急。比如下面的代碼讓一個UIButton變圓角了:
[self.button makeRoundedCorner:10.0f];
如圖:
按鈕
2. 讓整個界面變圓角
- 只要在需要添加圓角的
ViewContolloer
中調(diào)用這個方法就好了脓钾。
[self.view makeRoundedCorner:10.0f];
- 但是如果你的
ViewController
在UITabBarController
和UINavigationController
中,你就需要調(diào)用以下方法讓整個界面都變成圓角桩警。
[self.tabBarController.view makeRoundedCorner:10.0f];
[self.navigationController.view makeRoundedCorner:10.0f];
如圖:
界面圓角
好了可训,用法就是這樣,是不是很簡單呢捶枢,有需要的可以收藏一下沉噩。
將來的你,一定會感激現(xiàn)在拼命的自己柱蟀,愿自己與讀者的開發(fā)之路無限美好川蒙。