UI基礎(chǔ)

一.IBAction 和 IBOutlet

  • IBAction:
    • 本質(zhì)就是void
    • 能讓方法具備連線功能
  • IBOutlet
    • 能讓屬性具備連線功能

二.Storyboard連線出現(xiàn)的問題

  • 連接的屬性代碼被刪除,但連線沒有去掉.會(huì)報(bào)以下錯(cuò)誤:

    • setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
  • 連接的方法代碼被刪掉,但是連線沒有去掉.可能會(huì)出現(xiàn)方法找不到錯(cuò)誤

    • unrecognized selector sent to instance

三.項(xiàng)目屬性

  • Product Name
    • 軟件名稱、產(chǎn)品名稱、項(xiàng)目名稱
  • Organization Name
    • 公司名稱、組織名稱
  • Organization Identifier
    • 公司的唯一標(biāo)識(shí)
    • 一般是公司域名的反寫,比如com.xxxx
  • Bundle Identifier
    • 軟件的唯一標(biāo)識(shí)
    • 一般是Organization Identifier + Product Name

四.UIView常用屬性

@property(nonatomic,readonly) UIView *superView; // 獲得自己父控件對(duì)象
@property(nonatomic,readonly,copy) NSArray *subViews; // 獲得自己所有的子控制對(duì)象
@property(nonatomic) NSInteger tag; // 控件ID標(biāo)識(shí)符,可以通過對(duì)應(yīng)的tag來找對(duì)應(yīng)的控件
@property(nonatomic) CGAffineTransform transform; // 通過該屬性來設(shè)置控件旋轉(zhuǎn)角度,縮放,平移屬性
@property(nonatomic) CGRect frame; // 以父控件的左上角為坐標(biāo)原點(diǎn)
@property(nonatomic) CGRect bounds; // 以自己左上角為坐標(biāo)原點(diǎn),所以bounds的x.y一般為0
@property(nonatomic) CGPoint point; // 以父控件的左上角為坐標(biāo)原點(diǎn)(控件中心的位置)

- (void)addSubView:(UIView *)view; // 添加1個(gè)子控件
- (void)removeFromSuperView; // 從父控件中移除
- (UIView *)viewWithTag:(NSInteger) tag; // 根據(jù)1個(gè)tag找出對(duì)應(yīng)的控件
- (CGSize)sizeThatFits:(CGSize)size;  // 默認(rèn)是返回現(xiàn)有的視圖大小
- (void)bringSubviewToFront:(UIView *)view;  // 將控件顯示在最上面
- (void)sendSubviewToBack:(UIView *)view;  // 將控件顯示在最后面
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;  // 將控件插入到對(duì)應(yīng)的index位置
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;  // 將控件view顯示到子控件belowSubview的下面
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;   // 將控件view顯示到子控件aboveSubview的上面

五.UIButton的常見設(shè)置

- (void)setTitle:(NSString *)title forState:(UIControlState)state;  // 設(shè)置按鈕文字
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state ; // 設(shè)置按鈕文字顏色
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state; // 設(shè)置按鈕的小圖片
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state; // 設(shè)置按鈕背影圖片
- (nullable NSString *)titleForState:(UIControlState)state; //  獲取按鈕文字  
- (nullable UIColor *)titleColorForState:(UIControlState)state; //  獲取按鈕文字顏色
- (nullable UIImage *)imageForState:(UIControlState)state; //  獲取按鈕小圖片
- (nullable UIImage *)backgroundImageForState:(UIControlState)state; //  獲取按鈕背景圖片

六.UILabel的常見設(shè)置

@property(nonatomic,copy) NSString  *text;  // 顯示文字
@property(nonatomic,retain) UIFont  *font;  // 顯示字體
@property(nonatomic,retain) UIColor  *color;  // 顯示文字顏色
@property(nonatomic) NSTextAlignment  textAlignment;  // 對(duì)齊模式(左/中/右)
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize;  // 系統(tǒng)默認(rèn)字體
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;  // 粗體
+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;  // 斜體

七.定時(shí)任務(wù)

  • 方法1:performSelector
// 1s后自動(dòng)調(diào)用self的test方法
[self performSelector:@selector(test) withObject:nil afterDelay:1.5];
  • 方法2:GCD
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    // 1s后自動(dòng)執(zhí)行這個(gè)block里面的代碼
    self.hud.alpha = 0.0;
});
  • 方法3:NSTimer
// 1s后自動(dòng)調(diào)用self的hideHUD方法
[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(test) userInfo:nil repeats:NO];
// repeats如果為YES,意味著每隔1s都會(huì)調(diào)用一次self的hidHUD方法

八.常見問題

1. 控件看不見
- 寬度或者高度其實(shí)為0
- 位置不對(duì)(比如是個(gè)負(fù)數(shù)或者超大的數(shù),已經(jīng)超出屏幕)
- hidden == YES
- alpha <= 0.01
- 沒有設(shè)置背景色、沒有設(shè)置內(nèi)容
- 可能是文字顏色和背景色一樣
2. 項(xiàng)目中某個(gè).m文件無法使用
    - 檢查:Build Phases -> Compile Sources
- 項(xiàng)目里面的某個(gè)資源文件(比如plist、音頻等)無法使用
    - 檢查:Build Phases -> Copy Bundle Resources
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容