iOS 無規律總結UI基礎篇

zhuzheng 7年前發布 | 11K 次閱讀 iOS開發 移動開發 UITableView

頭尾式動畫(了解)

[UIView beginAnimations:nil context:nil];
準備開始動畫
[UIView setAnimationDuration:5];
設置時間
[UIView commitAnimations];
提交動畫(真正開始做動畫)

塊動畫

+(void)animateWithDuration:(NSTimeInterval)duration
                 animations:(void (^)(void))animations
                 completion:(void (^__nullable)(BOOL finished))completion;

+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;

+(void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^__nullable)(BOOL finished))completion;

duration : 動畫時間 delay : 延遲時間 dampingRatio : 阻尼系數(彈性) 越小越彈 velocity : 速率 options : 選項 animations : 做動畫的代碼塊 completion : 動畫完成的代碼塊 "回調"</code></pre>

View視圖插入當前視圖的上面和下面

- -(void)insertSubview:(UIView )view belowSubview:(UIView )siblingSubview;

- 再siblingSubview下面添加view
  • -(void)insertSubview:(UIView )view aboveSubview:(UIView )siblingSubview;
    • 再siblingSubview上面添加view</code></pre>

      序列幀動畫實現

      方法1
        [UIImage animatedImageWithImages:**動畫數組** duration:**持續時間**]; // 可以獲取一個能做動畫的UIImage對象
      方法2
         self.imageView.animationImages = array; // 裝圖片的數組(需要做動畫的圖片數組)
         self.imageView.animationDuration = 2; // 動畫時間
         self.imageView.animationRepeatCount = 1; // 重復次數 0 表示重復 
        [self.imageView startAnimating]; // 開始序列幀動畫

      圖片瀏覽器-內存問題

      通過imageNamed: 方法建立的圖片,系統會進行緩存,程序員無法銷毀.
      通過imageWithContentsOfFile: 建立的圖片,使用完成之后,會自動被釋放.

如何選擇圖像方法:

常用的圖像,(小的按鈕/背景)素材,放在 Assets 中,使用 imageNamed 加載,性能高 臨時使用的圖像,放在 Bundle 中,獲取其本地路徑,使用 imageWithContentsOfFile 加載,使用完成立即釋放!</code></pre>

兩種加載xib的方式

從 NSBundle加載XIB,只需要在第一個參數傳入 XIB 的文件名,注意:沒有擴展名
方法1,iOS 2.0 的方法
    UIView *appView = [[NSBundle mainBundle] loadNibNamed:@"CZAppView" owner:nil options:nil].lastObject;
方法2,iOS 4.0 的方法,做了內存優化
1.XIB 的文件名
    UINib *nib = [UINib nibWithNibName:@"CZAppView" bundle:nil];
2.Bundle 名,如果傳入 nil,會自動從 mainBundle 獲取
    UIView *appView = [nib instantiateWithOwner:nil options:nil].lastObject;

兩種加載文件的方式

方法1,通過文件路徑加載(本地)
    NSString *path = [[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"文件后綴"];
    NSArray *images = [NSArray arrayWithContentsOfFile:path];
方法2,通過文件 URL 加載(本地/網絡)
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"文件名.后綴" withExtension:nil];//或者后綴寫在后面
    NSArray *images = [NSArray arrayWithContentsOfURL:url];

UIScrollView

//如果要想要一個scrollView滾動,必須要設置它的滾動大小
    scrollView.contentSize = pictureImage.frame.size;

//設置scrollView上左下右的間距
scrollView.contentInset = UIEdgeInsetsMake(20, 30, 40, 50);

//禁用彈簧效果
scrollView.bounces = NO;

//禁用scorllView的手動滾動
scrollView.scrollEnabled = NO;

//滾動指示器是scrollView的兩個UIImageView類型的子控件。
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;

//scrollView滾動的本質就是scrollView的bounds的坐標的變化。
NSLog(@"%@",NSStringFromCGRect(self.scrollView.bounds));</code></pre> 

ScrollView滾動動畫處理

[scrollView setContentOffset: animated:YES];

調用這個方法如果animated這一參數設置為NO,或者直接設置contentOffset這個property,
delegate會收到一個scrollViewDidScroll:消息。
如果animated這一參數設置為YES,則在整個動畫過程中,
delegate會收到一系列的scrollViewDidScroll:消息,
并且當動畫完成時,還會收到一個scrollViewDidEndScrollingAnimation:消息。


使用UIView動畫后,無論在什么時候查詢contentOffset的值,得到的都是動畫的最終值
而使用animated參數,可以在動畫過程中得到與當前狀態較為一致的contentOffset值</code></pre> 

打印結構體
    NSStringFromCGRect(self.scrollView.bounds)

UIScrollView代理方法

只要滾動scrollView就會調用這個方法

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView{ NSLog(@"%s,contentOffset = %@",FUNCTION,NSStringFromCGPoint(scrollView.contentOffset));
    }</code></pre>
    scrollView將要開始被拖拽的時候就會調用這個方法
  • (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ NSLog(@"%s",FUNCTION); }</code></pre>
    scrollView將要停止拖拽的時候調用
  • (void)scrollViewWillEndDragging:(UIScrollView )scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint )targetContentOffset{ NSLog(@"%s",FUNCTION); }</code></pre>

    scrollView已經停止拖拽的時候調用
    decelerate 是否減速

  • (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ NSLog(@"%s,%d",FUNCTION,decelerate); }</code></pre>

    // 指定scrollView子控件的最大放大比例和最小縮小比例
      self.scrollView.maximumZoomScale = 1.1;
      self.scrollView.minimumZoomScale = 0.9;

// 返回需要縮放的視圖

  • (UIView )viewForZoomingInScrollView:(UIScrollView )scrollView{ return self.pictureImage; }</code></pre>

    UIView 的ContentMode

    UIViewContentModeScaleToFill          //縮放并填充
    UIViewContentModeScaleAspectFill      // 保持等比縮放填充
    UIViewContentModeScaleAspectFit       //保持寬高比同時讓圖片完全顯示

    UIPageControl

    //設置非當前的顏色
      pageControl.pageIndicatorTintColor
      //設置當前頁的顏色
      pageControl.currentPageIndicatorTintColor
      //設置pageControl的總頁數
      pageControl.numberOfPages

    代理和block

    代理

      1. 設置代理協議
      2. 指定代理方法
      3. 創建代理對象
      4. 遵守代理協議 -> 代理對象賦值
      5. 實現代理方法
    
    

    //被optional修飾的方法,調用前需要進行判斷。那樣,即使代理沒有實現這個方法,程序也不會崩潰 if ([self.delegate respondsToSelector:@selector(xxxx)]) {

      [self.delegate xxxx];
    

    }

    //被required修飾的方法,調用之前不進行判斷。那樣,如果代理沒有實現這個方法,程序立刻就崩潰

      [self.delegate xxxx];
Block的定義格式
返回值類型(^block變量名)(形參列表) = ^(形參列表) {

};

調用Block保存的代碼
block變量名(實參);

// typedef 定義的block
typedef NSString *(^myBlock)(NSString *name);</code></pre> 

導航欄

//  隱藏頂部狀態欄

- (BOOL)prefersStatusBarHidden{
    return YES;
}
//  設置狀態欄的樣式(黑或白)
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}</code></pre> 

UITableView

//一共有多少組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

//每組有多少行

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

//每一行顯示的內容 //這個方法里,一定不能返回nil

  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

//設置分組的頭部文字

  • (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section

// 設置分組的底部文字

  • (NSString )tableView:(UITableView )tableView titleForFooterInSection:(NSInteger)section

// tableView的樣式,在tableViw創建的時候就已經確定好了,不能夠修改! UITableView *homeTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];

// 錯誤的演示 style是一個只讀屬性 self.tableView.style = UITableViewStylePlain;

// Grouped 是有組頭和組尾 不會懸停 // Plain 會懸停,需要設置組頭信息

//設置tableView右側的索引標簽 -> 返回的字符串數組是什么樣就顯示什么樣,跟數據源的組只有位置的關系,點第一個就是第一組

  • (NSArray<NSString > )sectionIndexTitlesForTableView:(UITableView *)tableView

//設置 'headerView' 'FooterView' 只需要設置高度即可 tableView.tableHeaderView tableView.tableFooterView

// 設置組頭

  • (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section

// 設置組頭高度 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//不能寫0,寫0不起作用。 //0代表默認的組頭高度。 return 0.1; }

//在使用自動布局以前,如果要設置每個cell不同的高度,就需要在這里設置每個cell的高度。

  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{
    return 60; }

// 刷新 [1,2,3] 行 ->動畫效果 同下面不可同時存在 [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

// 插入 [1,2,3] 行 -> 動畫效果 [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];

//1.設置預估行高 tableView.estimatedRowHeight = 400; //2.設置tableView的行高為自動計算 tableView.rowHeight = UITableViewAutomaticDimension;

// 分割線 UITableViewCellSeparatorStyleNone, 沒有分隔線 UITableViewCellSeparatorStyleSingleLine, 有分隔線 UITableViewCellSeparatorStyleSingleLineEtched 有內容的沒有分隔線,沒有內容有分隔線。

tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // 可以調用系統的代碼設置分隔線 tableView.separatorColor = [UIColor redColor]; // 可以設置分割線的位置 tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); // UIEdgeInsetsMake CGFloat top, left, bottom, right;

//當tableView被拖拽的時候,隱藏鍵盤 self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

//如果使用自動布局,這個方法寫在viewDidLoad里是不行的。 一開始就滾動到底部 [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:最后一行 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];</code></pre>

UITableViewCell復用

獲取一個復用的Cell有兩種方法:

[tableView dequeueReusableCellWithIdentifier:] 必須要對獲取到的Cell進行非空判斷,如果為空就需要自己定義一個cell.

[tableView dequeueReusableCellWithIdentifier: forIndexPath:]; 使用之前必須要對cell進行注冊。如果注冊以后,可以不進行非空判斷。

注冊cell有三種方法: 1.使用xib進行注冊
self.tableView registerNib: forCellReuseIdentifier: //如果使用的是xib進行的注冊,創建cell的時候,就會調用 [NSBundle mainBundle]loadNibName:

 2.使用class類來進行注冊
 [self.tableView registerClass:[HMHeroCell class] forCellReuseIdentifier:cellID];
//如果使用的是class進行的注冊,創建cell的時候,會調用  [class alloc]initWithStyle:defautl樣式 reuseIdentifier

 3.關聯一個storyboard里的原型cell(prototypeCell)
 //如果使用的是storyboard進行的注冊,創建cell的時候,會直接加載storyboard里的原型cell.

//設置Cell右側的箭頭
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

//可以給cell右側設置一個accessoryView. cell.accessoryView = [[UISwitch alloc]init];</code></pre>

字典轉模型KVC

[self setValuesForKeysWithDictionary:dict];

本質就是 -> 定義好屬性,然后字典取出對應的值進行賦值.這就是為什么模型中的屬性一定不能錯一個字母的原因 _title = dict[@"title"]; _cars = dict[@"cars"];</code></pre>

16進制顏色轉換成RGB

hex = 0xA3B2FF;
    int red = hex & 0xFF0000 >> 16;
    int green = hex & 0x00FF00 >> 8;
    int blue = hex & 0x0000FF;

>>右移將一個數的各二進制位右移N位,移到右端的低位被舍棄,對于無符號數,高位補0

&按位與如果兩個相應的二進制位都為1,則該位的結果值為1,否則為0</code></pre> 

畫一個 一個像素的UIView

[UIScreen mainScreen].scale  獲取到屏幕的縮放比例
1 / [UIScreen mainScreen].scale  即可

UICollectionViewLayout

//如果創建的是 UICollectionViewLayout,返回的cell的方法不會被調用!!!
    創建這個    UICollectionViewFlowLayout

//設置collectionVioew組與組之間的間距
self.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10);

//設置同一組內,cell之間的間距。
//如果是垂直滾動,這個值代表的是水平最小間距
//如果是水平滾動,這個值代表的是垂直最小間距
self.minimumInteritemSpacing = 27.5;

//設置同一組內的"行"間距
//如果是垂直滾動,這個值代表的是垂直最小間距
//如果是水平滾動,這個值代表的是水平最小間距
self.minimumLineSpacing = 50;

//設置item的大小
self.itemSize = CGSizeMake(100,100);

//設置layout組頭和組尾的大小
//水平滾動的時候,寬度起作用
//垂直滾動的時候,高度起作用
self.headerReferenceSize = CGSizeMake(30, 50);
self.footerReferenceSize = CGSizeMake(50, 30);

// 自定義FlowLayout
    //這個方法在被調用的時候,collectionView已經有大小。
- (void)prepareLayout 
//一定要調用父類的方法
[super prepareLayout];  

// 會保存所有cell的indexPath和frame.
// 并不是一上來就把所有cell的frame都計算好,而是先計算一部分。
// 再滾動的時候,把剩下的部分給計算完,計算成以后添加到數組里。
// 它只會計算一次每個cell的大小,當再次滾動的時候,不會再重新計算,而是直接從數組里拿了數據顯示到collectionView上    
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect</code></pre> 

UICollectionViewDelegateFlowLayout

// 可以根據indexPath返回對應Cell的大小

  • (CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout )collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath</code></pre>

    UICollectionView 組頭組尾

    //collectionView初始化的時候,必須要傳入一個非空的布局參數!!!!

    對于段頭或者段尾等附加顯示的元素,同樣需要注冊: [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

    // 數據源

    • (UICollectionReusableView )collectionView:(UICollectionView )collectionView viewForSupplementaryElementOfKind:(NSString )kind atIndexPath:(NSIndexPath )indexPath

      // 進行獲取 reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];</code></pre>

      UIApplication基本使用

      //使用UIApplication對象,可以實現應用程序級別的操作。
      //例如:在桌面圖標上顯示消息個數、顯示網絡指示器、打電話、發短信、打開網頁

//iOS8.0以后,如果想要應用程序上顯示一個圖標,必須要先注冊。 if([[UIDevice currentDevice].systemVersion floatValue] >8.0){

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];

    [app registerUserNotificationSettings:settings];
}

//設置應用程序在手機桌面上顯示的消息個數 app.applicationIconBadgeNumber = 20;

//調用UIApplication的openURL方法,打開URL路徑 [[UIApplication sharedApplication] openURL:url];

//顯示網絡連接指示器 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

// 程序的啟動流程 //argc 參數的個數 //argv 參數的內容 //principalClassName 它必是UIApplication或者它的子類 nil代表的就是UIApplication //delegateClassName 它必須遵守UIApplicationDelegate協議 return UIApplicationMain(argc, argv, @"UIApplication", @"AppDelegate");

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions { //應用程序一啟動時就會調用這個方法 //如果是手寫代碼,可以在這里設置應用程序的Window和根控制器 NSLog(@"%s",FUNCTION); return YES; }

//應用程序已經進入到后臺以后會調用的方法

  • (void)applicationDidEnterBackground:(UIApplication *)application {

    NSLog(@"%s",FUNCTION); }

//應用程序將要進入前臺的調用的方法(第一次進行應用程序時,不會調用進入前臺的方法)

  • (void)applicationWillEnterForeground:(UIApplication *)application {

    NSLog(@"%s",FUNCTION); }

//應用程序已經獲取焦點(用戶可以交互)

  • (void)applicationDidBecomeActive:(UIApplication *)application {

    NSLog(@"%s",FUNCTION); }

//應用程序將要失去焦點(回到桌面的那一瞬間)

  • (void)applicationWillResignActive:(UIApplication *)application {

    NSLog(@"%s",FUNCTION); }

//應用程序將要被中止時調用

  • (void)applicationWillTerminate:(UIApplication *)application {

    NSLog(@"%s",FUNCTION); }

可以有兩個window,給Appdelegate加一個Window屬性 第一個 //創建一個控制器 MyViewController *controller = [[MyViewController alloc]init]; controller.view.backgroundColor = [UIColor redColor];

//設置控制器為window的主控制器
window.rootViewController = controller;

//把這個window設置為主window并且可見
[window makeKeyAndVisible];    
self.window = window;

第二個
self.window1 = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, 100, 200)];

MyViewController *controller1 = [[MyViewController alloc]init];
controller1.view.backgroundColor = [UIColor greenColor];
controller1.view.frame = [UIScreen mainScreen].bounds;
self.window1.rootViewController = controller1;

[self.window1 makeKeyAndVisible];</code></pre> 

UINavController導航控制器

// 狀態欄
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

//導航欄的高度是44. NSStringFromCGRect(navController.navigationBar.frame)

// 設置titleView 子控制器.navigationItem.titleView

// 設置title self.navigationItem.title = @"紅色視圖";

self.navigationItem.backBarButtonItem.title = @"紅色視圖";

self.title = @"紅色視圖";

//設置導航欄的標題文字顏色為白色 [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

//設置導航條barButtonItem的顏色 // self.navigationBar.tintColor = [UIColor orangeColor];

// 設置返回的樣式 self.navigationItem.backBarButtonItem

//UIBarButtonItem創建方法 系統樣式

  • (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action

自定義View

  • (instancetype)initWithCustomView:(UIView *)customView

自定義圖片

  • (instancetype)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action

自定義文字

  • (instancetype)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action

//設置navItem的左邊按鈕 self.navigationItem.leftBarButtonItems = @[item1,item2,item3];

//取消半透明效果。如果修改了translucent這個屬性,子控制器的坐標原點(0,0)會變化為(0,64) self.navigationController.navigationBar.translucent = NO;

//取消導航欄下面的分隔線 導航欄中有一個ImageView控件所以會顯示一條線 [self.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault]; [self.navigationBar setShadowImage:[[UIImage alloc]init]];

// 導航控制器跳轉 // push [self.navigationController pushViewController:greenController animated:YES];

// pop // 上一個

  • (UIViewController *)popViewControllerAnimated:(BOOL)animated

// 指定一個

  • (NSArray<__kindofUIViewController > )popToRootViewControllerAnimated:(BOOL)animated

// 跟控制器

  • (NSArray<__kindofUIViewController > )popToViewController:(UIViewController *)viewController animated:(BOOL)animated

// push的時候隱藏底部TabBar 一般寫在base導航控制器.根據子控制器的數量進行判斷

  • (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ if (self.viewControllers.count > 0) {
      viewController.hidesBottomBarWhenPushed = YES;
    
    } [super pushViewController:viewController animated:animated]; }</code></pre>

    圖片模式

    UIImage *image = [[UIImage imageNamed:@"圖片名稱"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

UIImageRenderingModeAlwaysTemplate// 模板 UIImageRenderingModeAlwaysOriginal// 默認 UIImageRenderingModeAutomatic// 自動</code></pre>

UITabBarController

// 指定子控制器
self.viewControllers =@[VC1,VC2];

self.childViewControllers 只讀的,不能夠賦值

controller.tabBarItem.title controller.tabBarItem.image// 默認 ->可以設置圖片模式 controller.tabBarItem.selectedImage// 選中 controller.tabBarItem.badgeValue// 小紅點</code></pre>

圖片切片

// 這個只是將要被拉伸1像素的橫線or豎線,其他的地方不會發生變化
image = [image stretchableImageWithLeftCapWidth:image.size.width0.5 topCapHeight:image.size.height  0.7];

// 指定圖片四根線組成的范圍。并指定是平鋪還是拉伸 image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right) resizingMode:UIImageResizingModeStretch]; UIImageResizingModeStretch// 拉伸 UIImageResizingModeTile// 平鋪</code></pre>

Masonry

mas_lessThanOrEqualTo(...)// 小于或等于
mas_greaterThanOrEqualTo(...)// 大于or等于

鍵盤

//控制中心 監測UIKeyboardWillChangeFrameNotification 鍵盤將要改變的信息.
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveKeyboardFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];

//重新接收鍵盤信息來自控制中心.

  • (void)receiveKeyboardFrameNotification:(NSNotification *)notification{

      /* notification = NSConcreteNotification 0x7fe70af0abe0 {name =    UIKeyboardDidChangeFrameNotification; userInfo = {
       UIKeyboardAnimationCurveUserInfoKey = 7;
       UIKeyboardAnimationDurationUserInfoKey = "0.25";
       UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
       UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";
       UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
       UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
       UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
       UIKeyboardIsLocalUserInfoKey = 1;
    

    }}*/

    //拿到鍵盤frame變化以后的結果
    CGRect endFrame = [notification.userInfo[@"UIKeyboardFrameEndUserInfoKey"]CGRectValue];

    //計算view的偏移 屏幕的高度 - 鍵盤frame變化以后的y值 CGFloat transform = [UIScreen mainScreen].bounds.size.height - endFrame.origin.y;

    //讓整個view偏移 [UIView animateWithDuration:0.25 animations:^{

      self.view.transform = CGAffineTransformMakeTranslation(0, -transform);
    

    }]; }

// 鍵盤消失 [self.textField resignFirstResponder]; //讓self.view或者它的textFiled子控件辭去第一響應者 [self.view endEditing:YES];</code></pre>

通知

//接收通知需要放在發送通知之前
//observer:接收者
//selector:接收到消息以后調用的方法(帶: 可以獲取到NSNotification 對象)
//name:接收的通知名稱
//obj:指的是發送消息的對象.nil表示不管是誰發送的消息都收

[[NSNotificationCenter defaultCenter] addObserver:接收者 selector:@selector(接收到消息以后調用的方法) name:@"接收的通知名稱" object:指的是發送消息的對象];

//name:消息的名稱 //object:誰發送的消息。這個方法只是單純的發送一個消息,不能夠發送消息的內容 [[NSNotificationCenter defaultCenter] postNotificationName:@"weather" object:tencent];

// 發送帶消息內容的通知,可以通過接收到的通知獲取userInfo中的消息內容 [[NSNotificationCenter defaultCenter] postNotificationName:@"weather" object:tencent userInfo:@{@"today":@"rainning",@"tomorrow":@"still rainning"}];

// 移除接受者 [[NSNotificationCenter defaultCenter]removeObserver接收者];</code></pre>

UIButton

在**storyBoard**中可以在屬性欄修改Button的title Insets 和ImageInsets 調整圖片和文字的位置

 

來自:http://www.jianshu.com/p/93eef73bf235

 

 本文由用戶 zhuzheng 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!