您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發:為 UIView 設置圓角與邊框

編輯:iOS 開發綜合

在 iOS 中,若要讓 UIView、UIButton、UIImageView 等元件顯示圓角,通常會透過其 layer 屬性設定。這方式簡單直接,很適合用於卡片、頭像、標籤與浮層。

基本設定

zheZhaoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
[zheZhaoView setBackgroundColor:[UIColor colorWithWhite:0.3f alpha:0.8f]];
zheZhaoView.layer.cornerRadius = 10;
zheZhaoView.layer.borderWidth = 0;
zheZhaoView.layer.borderColor = [[UIColor grayColor] CGColor];
zheZhaoView.layer.masksToBounds = YES;
[self addSubview:zheZhaoView];

參數說明

cornerRadius 控制圓角半徑,數值越大圓角越明顯。borderWidth 設定邊框寬度,borderColor 設定邊框顏色。masksToBounds 設為 YES 時,子檢視或內容會被裁切到圓角範圍內。

實務提醒

若只需要背景圓角,可保持 masksToBounds 為 NO;若圖片或子檢視也要跟著裁切,則需設為 YES。另外,大量動態更新圓角時,可搭配效能工具觀察是否造成繪製負擔。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved