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

iOS 項目筆記:常用 UI 元件屬性速查

編輯:iOS 開發綜合

把常用的 UIKit 元件屬性整理成一份速查表,寫程式的時候翻一下比翻文件快。

1. UIView

// 是否與使用者互動。設為 NO 則完全不接收觸控
@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled;

// 標記。父元件可透過 tag 找到對應的子元件
@property(nonatomic) NSInteger tag;

// 位置與尺寸,座標原點為「父元件的左上角」
@property(nonatomic) CGRect frame;

// 位置與尺寸,座標原點為「元件本身的左上角」
@property(nonatomic) CGRect bounds;

// 中心點位置,座標原點為父元件左上角
@property(nonatomic) CGPoint center;

// 形變:旋轉、縮放、平移
@property(nonatomic) CGAffineTransform transform;

// 父元件
@property(nonatomic, readonly) UIView *superview;

// 所有子元件
@property(nonatomic, readonly, copy) NSArray *subviews;

frame 與 center 是一組:改 frame 會動 center,改 center 只會動位置不改尺寸。而 transform 一旦不是單位矩陣,frame 就會變成「讀取時回傳估算值」,這時應該改用 bounds 與 center 來定位。

2. UILabel

@property(nonatomic, copy)   NSString *text;           // 顯示文字
@property(nonatomic, retain) UIFont *font;             // 字型
@property(nonatomic, retain) UIColor *textColor;       // 文字顏色
@property(nonatomic)         NSTextAlignment textAlignment;  // 對齊方式
@property(nonatomic) NSInteger numberOfLines;          // 行數,0 代表自動換行

3. UIImageView

@property(nonatomic, retain) UIImage *image;                  // 顯示的圖片
@property(nonatomic, copy)   NSArray *animationImages;        // 序列幀圖片陣列
@property(nonatomic) NSTimeInterval animationDuration;        // 動畫持續時間
@property(nonatomic) NSInteger animationRepeatCount;          // 播放次數,0 為無限循環

序列幀動畫要記得用 startAnimating/stopAnimating 啟動與停止。

4. UIScrollView

@property(nonatomic) CGPoint contentOffset;      // 目前捲動的位置
@property(nonatomic) CGSize contentSize;          // 內容尺寸,也就是可捲動的範圍
@property(nonatomic) UIEdgeInsets contentInset;   // 額外的邊緣滾動區域
@property(nonatomic, assign) id<UIScrollViewDelegate> delegate;

三者的關係值得記牢:contentSize 決定「能滾多遠」,contentOffset 是「目前滾到哪」,contentInset 則是「邊界還能再多拉一點」——下拉更新的橡皮筋效果就靠它。

5. UITableView 與 6. UIPickerView

這兩個元件的屬性與用法較為繁瑣,細節請參考前幾篇的專文說明。

7. UIControl

@property(nonatomic, getter=isEnabled) BOOL enabled;   // 是否可用

// 監聽元件內部事件:點擊、值改變等
- (void)addTarget:(id)target
           action:(SEL)action
 forControlEvents:(UIControlEvents)controlEvents;

UIDatePicker

@property(nonatomic) UIDatePickerMode datePickerMode;   // 模式/類型
@property(nonatomic, retain) NSLocale *locale;          // 區域設定(zh_CN 為中文)
@property(nonatomic, retain) NSDate *date;              // 目前時間

UISwitch

@property(nonatomic, getter=isOn) BOOL on;
- (void)setOn:(BOOL)on animated:(BOOL)animated;

UISegmentedControl

@property(nonatomic, readonly) NSUInteger numberOfSegments;   // 共有幾區
@property(nonatomic) NSInteger selectedSegmentIndex;           // 目前選中的索引

UISlider

@property(nonatomic) float value;          // 目前進度值
@property(nonatomic) float minimumValue;   // 最小值
@property(nonatomic) float maximumValue;   // 最大值

UIButton

+ (id)buttonWithType:(UIButtonType)buttonType;        // 快速建立
@property(nonatomic) UIEdgeInsets contentEdgeInsets;  // 內邊距
@property(nonatomic, readonly, retain) UILabel *titleLabel;
@property(nonatomic, readonly, retain) UIImageView *imageView;

// 這四組方法都要帶 state,是 UIButton 的核心設計
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

// 對應的取值得方法
- (NSString *)titleForState:(UIControlState)state;
- (UIColor *)titleColorForState:(UIControlState)state;
- (UIImage *)imageForState:(UIControlState)state;
- (UIImage *)backgroundImageForState:(UIControlState)state;

一個常見錯誤:只設 UIControlStateNormal 卻忘了設 UIControlStateHighlighted 或 UIControlStateDisabled,導致按下的回饋或停用時的外觀不如預期。

UITextField

透過 delegate 監聽內部事件(開始編輯、結束編輯、按下 Return)。

8. UIAlertView

/*
 title             :對話框標題
 message           :中間顯示的內容
 cancelButtonTitle :取消按鈕文字
 otherButtonTitles :其他按鈕文字,可設多個
 delegate          :監聽按鈕點擊
 */
- (id)initWithTitle:(NSString *)title
            message:(NSString *)message
           delegate:(id)delegate
  cancelButtonTitle:(NSString *)cancelButtonTitle
  otherButtonTitles:(NSString *)otherButtonTitles, ...;

- (void)show;

提醒:UIAlertView 與 UIActionSheet 在 iOS 8 之後已被 UIAlertController 取代,且前者在較新的 SDK 中已遭弃用。若是新專案,直接學 UIAlertController 即可。

補充:幾個跨元件通用的觀念

· enabled 與 userInteractionEnabled 不同。前者讓元件進入「停用」狀態(會切換到 disabled 的視覺樣式),後者是直接完全不接收事件。

· addTarget 是 UIControl 家族共通的機制,UIButton、UISwitch、UISlider 都靠它。而 UIView、UILabel 這類不繼承自 UIControl 的元件,要用 UITapGestureRecognizer 才能處理點擊。

· 所有 tag 都是全域的。同一個父元件下的子元件 tag 不該重複,否則 viewWithTag: 的結果無法預期。

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