明天就是元旦了 2016年啦 加油吧 明天你好!!
鍵盤監聽:
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong)UIToolbar *toolbar;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//監聽鍵盤顯示
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//
// // 鍵盤將要隱藏, 就會發出UIKeyboardWillHideNotification
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
self.toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 667-44, 375,44)];
self.toolbar.backgroundColor = [UIColor redColor];
[self.view addSubview:self.toolbar];
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 100,100)];
[self.view addSubview:tf];
//添加一個監聽鍵盤改變的通知
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
-(void)keyboardWillChange:(NSNotification *)noti{
// 1.獲取鍵盤彈出的時間
CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
NSLog(@"-=-= %f",duration);
// 2.動畫
[UIView animateWithDuration:duration animations:^{
// 取出鍵盤高度
CGRect keyboardF = [noti.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGFloat keyboardH = keyboardF.size.height;
if (keyboardF.origin.y==667) {//收鍵盤
self.toolbar.transform = CGAffineTransformIdentity;
}else{
self.toolbar.transform = CGAffineTransformMakeTranslation(0, -keyboardH);
}
}];
}
-(void)keyboardWillShow:(NSNotification *)noti{
// 1.獲取鍵盤彈出的時間
CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
// // 2.動畫
[UIView animateWithDuration:duration animations:^{
// // 取出鍵盤高度
CGRect keyboardF = [noti.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGFloat keyboardH = keyboardF.size.height;
//讓控件做位置偏移的 transform
self.toolbar.transform = CGAffineTransformMakeTranslation(0, -keyboardH);
}];
}
-(void)keyboardWillHide:(NSNotification *)noti{
// 1.獲取鍵盤彈出的時間
CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
// // 2.動畫
[UIView animateWithDuration:duration animations:^{
//還原
self.toolbar.transform = CGAffineTransformIdentity;
}];
}
對於IOS的app開發者來說,不會像Android開發者一樣爲很多的屏幕尺寸來做界面適配,因此硬編碼的座標也能工作良好,但是從設計模式上來說這不是好的做法。而且也還有一些問題,如iPhone5的適配,橫豎屏的切換等。或許你可以做兩套UI方案來做適配,但是這樣增加重複工作量,而且不夠高端,萬一有出新的屏幕大小了呢。哲理就將介紹IOS中的兩大自動佈局利器:Autoresizing
和 Autolayout
。 autoresizing是UIView的屬性,一直都有,使用簡單,但是沒有autolayout強大。autolayout是IOS6以後的新技術,更加強大。本文主要介紹Autoresizing
的特性和用法。
1. Autoresizing特性
當UIView
的autoresizesSubviews
是YES
時,(默認是YES), 那麼在其中的子view會根據它自身的autoresizingMask
屬性來自動適應其與superView
之間的位置和大小。
autoresizingMask
是一個枚舉類型, 默認是UIViewAutoresizingNone
, 也就是不會autoresize:
123456789
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) { UIViewAutoresizingNone = 0, UIViewAutoresizingFlexibleLeftMargin = 1 << 0, UIViewAutoresizingFlexibleWidth = 1 << 1, UIViewAutoresizingFlexibleRightMargin = 1 << 2, UIViewAutoresizingFlexibleTopMargin = 1 << 3, UIViewAutoresizingFlexibleHeight = 1 << 4, UIViewAutoresizingFlexibleBottomMargin = 1 << 5};
這個枚舉類型,使用了 1 << n
這樣的寫法來定義,代表了它可以複選。如果你不明白爲什麼,可以複習下“位運算”。 那麼這些值分別代表什麼意思呢?
其實如何理解這幾個值很簡單,那就是從xib裏面看。 我們在一個xib文件中,取消勾選autolayout
,(默認使用autolayout時,autoresizing看不到)。那麼我們可以在佈局那一欄看到如何設置autoresizing
.
上圖說明了在xib中設置的這些線條和實際屬性對應的關係,這其中需要注意的是,其中4個margin虛線才代表設置了該值,而width和height是實線代表設置了該值,不能想當然的理解。
這些項分別代表:
autoresizingMask是子視圖的左、右、上、下邊距以及寬度和高度相對於父視圖按比例變化,例如:
UIViewAutoresizingNone 不自動調整。
UIViewAutoresizingFlexibleLeftMargin 自動按比例調整與superView左邊的距離,且與superView右邊的距離不變。
UIViewAutoresizingFlexibleRightMargin 自動按比例調整與superView的右邊距離,且與superView左邊的距離不變。
UIViewAutoresizingFlexibleTopMargin 自動按比例調整與superView的頂部距離,且與superView底部的距離不變。
UIViewAutoresizingFlexibleBottomMargin 自動按比例調整與superView的底部距離,且與superView頂部的距離不變。
UIViewAutoresizingFlexibleWidth 自動按比例調整寬度。
UIViewAutoresizingFlexibleHeight 自動按比例調整高度。
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 40)];
[label setAutoresizingMask: UIViewAutoresizingNone]; 控件相對於父視圖座標值不變
CGRectMake(50, 100, 200, 40)
UIViewAutoresizingFlexibleWidth:控件的寬度隨着父視圖的寬度按比例改變 例如
label寬度爲 100 屏幕的寬度爲320 當屏幕寬度爲480時 label寬度 變爲 100*480/320
以上這些都較易理解, 但是autoresizing
還有一些組合場景。那就是組合使用的場景。
autoresizingMask
說明
xib預覽效果
None
view的frame不會隨superview的改變而改變(右圖的xib中預覽效果與實際效果有差,實際效果是view的上邊距不變)
TopMargin | BottomMargin
view與其superView的上邊距和下邊距的比例維持不變
LeftMargin | RightMargin
view與其superView的左邊距和右邊距的比例維持不變(右圖的xib中預覽效果與實際效果有差,實際效果是view的上邊距不變)
LeftMargin | RightMargin | TopMargin | BottomMargin
view與其superView的上下左右邊距的比例維持不變
LeftMargin | Width
view與其superView的右邊距的比例維持不變, 左邊距和width按比例調整(右圖的xib中預覽效果與實際效果有差,實際效果是view的上邊距不變)
LeftMargin | Width | RightMargin
左邊距、右邊距、寬按比例調整,(右圖的xib中預覽效果與實際效果有差,實際效果是view的上邊距不變)
Width | Height
自動調整view的寬和高,保證上下左右邊距不變。
上面並未列舉所有組合場景,但是已經足夠我們理解 autoresizing
了。
2. 小結
Autoreszing的最常見的實用場景就是iPhone5的兼容了。比如我們想要設置tableView的frame,那我們只需要在初始化設置frame之後將tableView的autoresizingMask設置爲UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight
就行了。
另一種比如我們想要一個view一直停留在其superview的最下方,那麼我們在初始化設置frame之後只需要將autoresizingMask設置爲UIViewAutoresizingFlexibleTopMargin
就可以了。
autorezingMask簡單的一個屬性,理解它之後可以讓很多事情變得簡單
學習ios 重要還是要理清楚思路 在做或者看老師代碼的時候 自己多想想爲什麼 不要自己看着就抄 另外還是要推薦一下 藍懿IOS這個培訓機構 和劉國斌老師劉國斌老師還是很有名氣的,聽朋友說劉老師成立了藍懿iOS,,老師講課方式很獨特,能夠儘量讓每個人都能弄明白,有的比較難懂的地方,如果有的地方還是不懂得話,老師會換個其它方法再講解,這對於我們這些學習iOS的同學是非常好的,多種方式的講解會理解得更全面,這個必須得給個贊,嘻嘻,還有就是這裏的學習環境很好,很安靜,可以很安心的學習,安靜的環境是學習的基礎,小班講課,每個班20幾個學生,學習氛圍非常好,每天都學到9點多才離開教室,練習的時間很充裕,而且如果在練習的過程中有什麼困難,隨時可以向老師求助,不像其它機構,通過視頻教學,有的甚至學完之後都看不到講師本人,問點問題都不方便,這就是藍懿與其它機構的區別,相信在劉國斌老師的細心指導下,每個藍懿學員都能找到滿意的工作,加油!
寫博客第八十二天;
QQ:565803433