基本控件
UIView
一般情況下我們會(huì)把它當(dāng)做容器來用
常用屬性-
backGroundColor //背景顏色
frame, bounds, center //位置 尺寸
tag //標(biāo)識(shí)
alpha //透明度
clipsToBounds
contentMode //內(nèi)容模式
userInteractionEnabled 默認(rèn)是YES
常用的方法
viewWithTag:
-(void)removeFromSuperview;
-(void)insertSubview: atIndex:;
-(void)exchangeSubviewAtIndex: withSubviewAtIndex:(NSInteger)index2;
-(void)addSubview:(UIView *)view;
-(void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
-(void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
-(void)bringSubviewToFront:(UIView *)view;
-(void)sendSubviewToBack:(UIView *)view;
UILabel
用來顯示只讀文本(不能編輯, 選中)
常用屬性
text
textColor
font
textAlignment
numberOfLines
shadowOffse
常用方法
Color
UIButton
一個(gè)用來相應(yīng)用戶交互的視圖
常用的屬性
UIButton中很多的屬性都是和狀態(tài)綁定的, 如果脫離了狀態(tài)直接在內(nèi)部的控件上設(shè)置是沒有效果的, E.G title, titleColor, BackGroundImagtate;
currentTitle, currentImage
常用方法
-(nullable NSString *)titleForState:(UIControlState)state;
-(nullable UIColor *)titleColorForState:(UIControlState)state;
-(CGRect)titleRectForContentRect:(CGRect)contentRect;
-(CGRect)imageRectForContentRect:(CGRect)contentRect;
父類方法: - addTarget:action:forControlEvents:
UIImageView
一個(gè)顯示圖片的視圖
常用屬性
image
highlightedImage
userInteractionEnabled 默認(rèn)是NO(重寫了父類的屬性)
animationImages
animationDuration
animationRepeatCount
常用方法
-(void)startAnimating;
-(void)stopAnimating;
-(BOOL)isAnimating;
UITextField
一個(gè)用來顯示/編輯單行文字的視圖
常用屬性
text
placeholder
leftView
leftViewMode
rightView
rightViewMode
attributedText
textAlignment
editing
inputView
inputAccessoryView
delegate
代理方法:
- // 當(dāng)用戶點(diǎn)擊鍵盤上的回車鍵的時(shí)候就會(huì)調(diào)用這個(gè)代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
- // 結(jié)束編輯的時(shí)候調(diào)用
-(void)textFieldDidEndEditing:(UITextField *)textField
- // 開始編輯的時(shí)候調(diào)用
-(void)textFieldDidBeginEditing:(UITextField *)textField
- // 是否允許進(jìn)入編輯狀態(tài)
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
- // 詢問String字符串是否應(yīng)該替換在range的位置
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
UITextView
一個(gè)用來顯示/編輯多行文字的視圖
常用屬性
text
textColor
font
textAlignment
inputView
inputAccessoryView
- 代理方法
- // 是否應(yīng)該開始編輯
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
- // 是否應(yīng)該結(jié)束編輯
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
- // 已經(jīng)開始編輯
-(void)textViewDidBeginEditing:(UITextView *)textView
- // 已經(jīng)結(jié)束編輯
-(void)textViewDidEndEditing:(UITextView *)textView
- // 是否在指定的范圍替換文本:text
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- // 文本已經(jīng)改變
-(void)textViewDidChange:(UITextView *)textView
- // 選中的范圍已經(jīng)改變
-(void)textViewDidChangeSelection:(UITextView *)textView
- // 是否在指定范圍內(nèi)相應(yīng)URL的點(diǎn)擊
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
- 鍵盤通知的Key
UIKeyboardWillShowNotification
UIKeyboardWillHideNotification
UIScrollView
一個(gè)可以滾動(dòng)的視圖
常用屬性
contentOffset : 內(nèi)容的偏移
contentSize: 內(nèi)容的尺寸
contentInset: 內(nèi)容的內(nèi)邊距
showsHorizontalScrollIndicator: 顯示水平的滾動(dòng)條
showsVerticalScrollIndicator: 顯示垂直的滾動(dòng)條
directionalLockEnabled: 鎖定某一個(gè)方向的滾動(dòng), 開始的方向是什么, 只要手指還在scrooView上拖動(dòng), 就一直是這個(gè)方向滑動(dòng)
bounces: 是否有彈簧效果, 只要滑動(dòng)到ContentView的邊緣就沒有再滑動(dòng)的空間
alwaysBounceVertical: 是否一直有彈簧效果: 使用范圍 ContentSize.height < ScrollView.height 的情況下垂直方向默認(rèn)情況下是沒有彈簧效果的, 但是設(shè)置了AlwaysBounceVertical = YES的時(shí)候就會(huì)讓在這種情況下仍然有彈簧效果
alwaysBounceHorizontal
decelerationRate: 減速的速率
pagingEnabled: 分頁
- 代理方法
- // 只要scrollView在滑動(dòng)就會(huì)一直調(diào)用, 調(diào)用非常頻繁
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
- // 將要開始滑動(dòng)
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
- // 將要停止拖動(dòng)的時(shí)候, 手指即將松開的時(shí)候調(diào)用 velocity: 這個(gè)時(shí)候的速度 targetContentOffset: 停止?jié)L動(dòng)的時(shí)候最終偏移量
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
- // 已經(jīng)結(jié)束拖動(dòng)
// decelerate: 告訴你是否要減速
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
- // scrollView已經(jīng)滾動(dòng)到最頂部
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
- // 即將開始減速
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
- // 已經(jīng)完成減速
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
UITableView
一個(gè)用來展示和編輯層級(jí)列表信息的視圖
常用屬性
sectionHeaderHeight
sectionFooterHeight
rowHeight
separatorColor
separatorInset
separatorStyle
tableHeaderView
tableFooterView
- 數(shù)據(jù)源方法/代理方法
- // tableView中有幾個(gè)Section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- // 某個(gè)section中有幾個(gè)cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- // 每個(gè)row顯示的內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- // 某個(gè)指定的Section的header的標(biāo)題
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- // 某個(gè)指定的Section的footer的標(biāo)題
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
- // 設(shè)置指定Section header上顯示的view, 有了view之后, title就不起作用, 就是一個(gè)優(yōu)先級(jí)的問題
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- // 設(shè)置指定Section footer上顯示的view
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
- // 設(shè)置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- // Section中header的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- // Section中footer的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
- // 選中了某個(gè)cell(cell是顯示的內(nèi)容, 繼承自UIView, row: 行號(hào))
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
傳值
正向傳值
A向B傳值
屬性傳值
- 在A中創(chuàng)建一個(gè)B的實(shí)例對(duì)象, B要有一個(gè)寫好的屬性, 然后在A中給B的屬性賦值
逆向傳值
- B向A傳值
Block
A中創(chuàng)建B的實(shí)例對(duì)象
在B中寫一個(gè)Block屬性
B中當(dāng)事件觸發(fā)的時(shí)候調(diào)用Block(判斷Block屬性是否有值)
A中設(shè)置Block屬性, 也就是在創(chuàng)建B的地方寫一個(gè)Block賦值給B的Block屬性
注意點(diǎn): Memory Cycle: 引用循環(huán), 解決方法: __weak
__weak typeof(self) selfWeak = self;
如果Block里面訪問的是成員變量, 在Block里面先用這個(gè)類型接收一下這個(gè)__weak指向的變量,再用這個(gè)新接收的類型來進(jìn)行成員變量的訪問
如果訪問的是屬性, 可以直接使用selfWeak.屬性名 直接訪問
Delagate
A中創(chuàng)建B的實(shí)例對(duì)象
在B中要先定義一個(gè)協(xié)議(協(xié)議的方法中第一個(gè)參數(shù) 一般情況下是 遵守協(xié)議的屬性[delegate]所在的對(duì)象B)
在B中寫一個(gè)遵守寫好協(xié)議的對(duì)象, 就是delegate
在B中當(dāng)事件觸發(fā)的時(shí)候調(diào)用協(xié)議方法(需要判斷這個(gè)代理是否實(shí)現(xiàn)這個(gè)協(xié)議方法)
在A中設(shè)置代理, 遵守協(xié)議, 實(shí)現(xiàn)協(xié)議中的方法
Notification
當(dāng)B中的事件觸發(fā)的時(shí)候, 發(fā)送(post)一個(gè)通知
在A中添加一個(gè)監(jiān)聽器A
當(dāng)A銷毀的時(shí)候, 移除監(jiān)聽器
控制器的展現(xiàn)方式
模態(tài)
一般是臨時(shí)展示某一個(gè)控制器的視圖
用法: 調(diào)用控制器的
-(void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion // viewControllerToPresent:將要展示的控制器 completion:完成跳轉(zhuǎn)之后要執(zhí)行的Block代碼塊
-(void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion // flag: 消失的時(shí)候是否需要?jiǎng)赢嬓Ч?completion: 執(zhí)行小時(shí)動(dòng)作完成之后要執(zhí)行的Block代碼塊
UINavigationController
一般用來展示導(dǎo)航層級(jí)結(jié)構(gòu)的內(nèi)容的
用法
一個(gè)控制器要想使用push, 則必須被UINavigationController或者其子類管理
展示一個(gè)新的控制器:
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
-(nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;
設(shè)置導(dǎo)航欄上的各種屬性
設(shè)置導(dǎo)航欄上面的各種屬性的時(shí)候推薦大家使用 Appearance的方式, 只要設(shè)置一次所有的UINavigationBar就會(huì)有同一種樣式, 不用每一個(gè)都設(shè)置
調(diào)用UINavigationBar的appearance方法就會(huì)返回一個(gè)UINavigationBar對(duì)象, 在這個(gè)對(duì)象中就可以使用UINavigationBar里面所有標(biāo)識(shí)為UI_APPEARANCE_SELECTOR的方法和屬性.
UITabBarController
一個(gè)用來顯示單選視圖的控制器
用法:
方法一:
調(diào)用TabBarController的addChildViewController:方法添加一個(gè)控制器
方法二:
設(shè)置TabBarController的viewControllers屬性, 就是把所有的控制器先創(chuàng)建好, 然后全部放在一個(gè)數(shù)組中, 最后把這個(gè)數(shù)組賦值給viewControllers這個(gè)屬性
設(shè)置UITabBarItem
image
selectedImage
title
titlePositionAdjustment: 文字的偏移