iOS UIView

UIView是負責在iOS裏面顯示一切內容的容器,UIView是所有View視圖的父類,包括UIWindow,也是UIView的子類。

1.UIView 重要屬性

frame // 指定視圖的座標和大小,相對於父視圖的座標系;
bounds // 邊界屬性,定義視圖的位置和大小,是相對於自身的座標系;
center // frame的中心座標,相對於父視圖的座標系;
alpha // 透明度;
backgroundColor // 視圖的背景顏色;
userInteractionEnabled // 是否可以響應點擊(父視圖如果不接受用戶交互事件,子視圖同樣也不會接受用戶事件);
autouresizingMask // 如何調整自己的大小,當superView的bounds發生改變時;
autouresizsSubviews // 是否自動調整subviews的大小,默認爲YES;
clipsToBounds // 子視圖超出父視圖bounds後,是否裁減,默認爲false;
// (如果子視圖超出了父視圖的範圍,默認情況下在界面上子視圖會全部顯示出來,但超出的範圍不會接受到用戶事件)

每個view都有一個tag屬性,可以通過它來快速定位某個subview;

-(UIView *)viewWithTag:(NSInteger)tag;
-(void)setTag:(NSInteger)tag;

View圓角

view.layer.cornerRadius = 5;  // 圓角半徑
view.layer.masksToBounds = YES;  // 裁剪layer多餘的部分

如果在代碼中設置圓角無效,可參考:在initWithCoder中,設置view.layer.masksToBounds無效,則還需要在awakeFromNib中再設置;

2.UIView 常見方法

initWithFrame:(CGRect)frame // 初始化
addSubview: // 添加視圖
insertSubview:atIndex: // 插入視圖
insertSubview:aboveSubview:
insertSubview:belowSubview:
sendSubviewToBack:
exchangeSubviewAtIndex:withSubviewAtIndex:
removeFromSuperview: // 移除視圖
setNeedsLayout: // 強制重新佈局子控件(此方法會自動調用layoutSubviews)
[view performSelector: withObject: afterDelay: ]; // 延遲一段時間執行某個方法;
[view.subviews makeObjectsPerformSelector: withObject:]; // 讓view的全部subview執行某一個方法,selector(方法名);
-(void)bringSubviewToFront:(UIView *)view // 把某一個view(v)顯示在父view的最上層;

3.initWithCoder和awakeFromNib

當UIView從xib或者storyboard中創建好並且可以使用該控件時,會調用awakeFromNib方法;因爲從xib或者storyboard中創建,其實就是從文件中創建,即是通過歸檔文件來創建UIView的,所有也會執行initWithCoder方法,並且是在awakeFromNib方法前執行;如果使用自定義view,則在initWithCoder方法裏面需要調用[super initWithCoder];
在initWithFrame和initWithCoder兩個方法裏面都不能準確獲取View的frame,如果要對子view進行frame設置,則需要在layoutSubviews方法裏面來對subview設置frame或其它屬性;
當UIView是通過代碼創建的時候,也會調用initWithFrame方法;

4.代理delegate

UIView控件的代理屬性的標識必須爲weak;因爲本身把一個UIView添加到一個controller,該controller就持有了該對象的強引用,如果再把控件的delegate屬性也使用strong的話,就會造成控件與控制器的循環引用,從而導致內存問題;
但並不是所有的代理都必須使用weak,其它情況下是可以使用strong的;

UILabel

numberOfLines = 0; // label會自動換行,根據需要顯示的內容來決定行數;
lineBreakMode = NSLineBreakByWordWrapping; // 換行模式;

通常兩個屬性要同時設置;

UITextField(只有一行)

placeholder //  hint 提醒
keyboardType // 鍵盤類型
secureTextEntry = YES; // 密碼輸入
contentVerticalAlignment; // 垂直對齊方式(父類的屬性);
delegate // 用戶交互事件,是一個協議,可以實現裏面的方法來監聽輸入情況等;
Clear Button;  // 編輯時顯示在右側的刪除按鈕;
inputView;  // 指定當UITextField獲取焦點之後,彈出的輸入視圖;
inputAccessoryView;  // UITextField獲取到焦點之後的輔助視圖(可用於自定義鍵盤);
[textField becomeFirstResponder]; //  手動打開鍵盤
// 設置內容對齊方式(此屬性是UIControl的屬性)
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  // 居中
UIImageView *leftView;
leftView.contentMode = UIViewContentModeCenter;  // 設置leftView的內容居中;
textField.leftView = leftView;  // leftView需要設置size屬性;
textField.leftViewMode = UITextFieldViewModeAlways;  // 左邊的view永遠顯示;

監聽文字改變,可以使用delegate代理,也可以通過監聽通知:

UITextViewTextDidChangeNotification;

UITextView

hasText:是否包含文字
text屬性只包括普通文本,attributedText包括顯示在textView裏面的所有內容;
// 設置字符選中範圍
textview.selectedRange = NSMakeRange(20, 10);
// 算出選中的字符範圍的邊框
NSArray *rects = [textview selectionRectsForRange:textview.selcetedTextRange];
for(UITextSelectionRect *rect in rects){
}

UIButton

Highlighted Adjusts Image  // 禁止高亮狀態image調整;
adjustsImageWhenHighlighted
currentTitle;  // 當前狀態下的文字;
selected = YES | NO

如果要去除UIButton的選中狀態,可繼承UIButton重寫setHighlighted方法,置空該方法,不調用[super setHighlighted];
UIButton默認圖標顯示在左邊,如果需要顯示在其它位置,需要自定義UIButton並重寫下面兩個方法。

titleRectForContentRect:(CGRect)contentRect  // 用於返回按鈕上標題的位置;
imageRectForContentRect:(CGRect)contentRect  // 返回圖標在按鈕上的位置;

按鈕內容對齊方式

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; // 左對齊

UISlider

UISlider是iOS中的滑塊,多見於控制音量,進度等(官方指定大小爲34);
UISlider和UIButton都是繼承自UIControl的子類,能響應事件,通常我們使用valueChanged這種eventType來控制交互事件;有三個常用屬性最大值,最小值,以及當前的值;
1.attribute

minimumValueImage  // 指定滑塊左邊的橫線爲圖片(同理右邊爲max,下不復述)
minimumTrackTintColor  // 指定滑塊左邊的橫線顏色
thumbTintColor  // 滑塊高亮狀態顏色
currentThumbImage  // 滑塊thumbImage

2.function

-minimumTrackImageForState:  //
-thumbImageForState:  // 
-setThumbImage:forState:  //

UIToolBar

UIToolbar 上只能添加UIBarButtonItem;

UIDatePicker

常用屬性

datePickerMode; // 日期模式;
local; // 顯示語言;

設置選擇改變監聽事件

[datePicker addTarget:self action:@selector(test:) forControlEvents:UIControlEventValueChanged];

UIImageView

animationImages;  // 一個動畫圖片數組,UIImageView可以播放多張圖片;
animationDuration;  // 動畫時長,默認爲1s;
animationRepeatCount;  // 動畫重複次數;
// 注意:播放多張圖片,需要調用startAnimating來播放;
highlightImage;  // 

UIScrollView

1.UIScrollView無法滾動的可能原因

沒有設置contentSize;
scrollEnabled = NO;
沒有接收到觸摸事件:userInteractionEnabled = NO;
沒有取消autolayout功能,(要想scrollview滾動),在xcode5.x下必須取消autolayout;

2.重要屬性

CGPoint contentOffset;  // 內容相對於UIScrollView左上角的偏移量
UIEdgeInsets contentInset;  // 內容的內邊距
CGSize contentSize;  // 內容的真實大小,某個方向上不希望滾動,則可以把該方向上的contentSize值設置爲0
Bool bounces;  // 彈簧效果
Bool ScrollEnabled;  // 是否能滾動
Bool showHorizontalScrollIndicator;  // 顯示水平滾動條
alwaysBounceVertical = YES;  // 垂直方向上有彈簧效果

3.通過UIScrollView實現縮放
向UIScrollView中添加內容(也就是指要進行縮放的內容)
設置縮放比例,最大、最小比例
通過代理監聽縮放事件,在縮放事件中返回UIScrollView的某個子控件(這個子控件就是告訴UIScrollView對這個控件進行縮放)

4.分頁
通過設置UIScrollView的pagingEnabled爲YES,UIScrollView會被分成多個獨立頁面,分頁的每一頁寬度爲UIScrollView的寬度;通常會配合UIPageControl增加分頁效果;

5.滾動/自動滾動
通過定時器(NSTimer)實現自動滾動

UIAlertView

如果想要一個alertView顯示文本框,則需要修改alertView的樣式;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView textFieldAtIndex:0]; // 根據索引獲取alertView中指定的某個文本框

UIActionSheet

UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"title" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定" otherButtonTitles: nil];
[sheet showInView:];

UIActivityIndicatorView

默認情況下不會滾動,如果需要它自動滾動,需要勾選Behavior的Animating屬性;

UIPageControl

numberOfPages;  // 頁數
currentPage;  // 當前頁
hidesForSinglepage;  // 只有一頁時,是否需要隱藏頁碼指示器
pageIndicatorTintColor;  // 頁碼指示器顏色
currentPageIndicatorTintColor;  // 當前頁碼指示器顏色

通過KVC改變_currentPageImage和_pageImage;

UIPickerView

UIPickerView有默認的frame屬性,它的高度是固定的;
常用代理方法:

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// 返回列數;
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
// 返回指定component列的行數;
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
// 指定的某行某列需要顯示的字符串內容;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
// 某一行被選中的回調事件,只有通過手指選中某一行的時候纔會被調用;
[pickerView selectRow:1 inComponent:0 animated:YES];  // 使pickerView主動選中某一行,該方法不會觸發pickerView:didSelectRow:inComponent:;
[pickerView selectedRowInComponent:0];  // 獲取某一列的當前被選中行;
[pickerView reloadAllComponents];  // 重新加載全部數據;
[pickerView reloadComponent:];  // 重新加載指定列的數據;

自定義UIPickerView的指定行和指定列的View,最後一個參數是當有可以重用的view的時候,會把該view自動傳回來;自定義UIView時,可以不指定它的寬高,它有默認的寬高;

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component  // 返回指定的某一列的行高
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章