iOS開發常用筆記整理

1.去掉tableview header的黏性

- (void)scrollViewDidScroll:(UIScrollView*)scrollView{ ?

? ?[self.searchBar resignFirstResponder];

if (scrollView== self.listTableView) {? ? ?

? ?CGFloat sectionHeaderHeight = HEADVIEW_HIGHT;

? ? ? ? ?if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {

? ? ? ? ? ? ? ? ? ? scrollView.contentInset= UIEdgeInsetsMake(-scrollView.contentOffset.y,0,0,0);

? ? ? ? ? ? ? ? } else if (scrollView.contentOffset.y>=sectionHeaderHeight) ?{

? ? ? ? ? ? ? ? ? ? ? scrollView.contentInset= UIEdgeInsetsMake(-sectionHeaderHeight,0,0,0);

? ? ?}?

? }

}

2.拖動tableView時收起鍵盤

只有一行代碼:

tableView.keyboardDismissMode =UIScrollViewKeyboardDismissModeOnDrag;

其中keyboardDismissMode是UIScrollView的屬性,

它的值除了UIScrollViewKeyboardDismissModeNone,

還有一種是UIScrollViewKeyboardDismissModeInteractive,

表示鍵盤可以隨著手指下滑而移出屏幕

既然是UIScrollView的屬性,

那么在文字常用的UITextView等控件中也可以使用.

3.去掉tableview滾動條

//隱藏滾動條self.tableView.showsVerticalScrollIndicator =NO;

4.字符串反轉

//第一種方法:

- (NSString*)reverseWordsInString:(NSString*)str{

? ? ? ? NSMutableString*newString = [[NSMutableStringalloc] initWithCapacity:str.length];

? ? ? ?for(NSIntegeri = str.length -1; i >=0; i --)? ? {? ? ? ??

? ? ? ? ? ? ? ? unicharch = [strcharacterAtIndex:i];? ? ? ? ? ? ?

? ? ? ? ? ? ? ? [newStringappendFormat:@"%c", ch];? ? ? ?

? ? ? ? }

return newString;

}

//第二種方法:

- (NSString*)reverseWordsInString:(NSString*)str{

? ? ? ? ? ? NSMutableString*reverString = [NSMutableStringstringWithCapacity:str.length];? ? ? ?

? ? ? ? ? ? [strenumerateSubstringsInRange:NSMakeRange(0, str.length) options:NSStringEnumerationReverse|NSStringEnumerationByComposedCharacterSequencesusingBlock:^(NSString*substring,NSRangesubstringRange,NSRangeenclosingRange,BOOL*stop) {? ? ? ? ??

? ? ? ? ? ?[reverStringappendString:substring];? ? ? ? ? ? ? ? ? ? ? ? ? ??

? }];

return reverString;

}

5.NSString使用stringWithFormat拼接的相關知識

保留2位小數點

//.2代表小數點后面保留2位(2代表保留的數量)

NSString*string = [NSStringstringWithFormat:@"%.2f",M_PI];

//輸出結果是: 3.14NSLog(@"%@", string);

用0補全的方法

NSInteger count =5;

//02代表:如果count不足2位 用0在最前面補全(2代表總輸出的個數)

NSString*string = [NSStringstringWithFormat:@"%02zd",count];

//輸出結果是: 05

NSLog(@"%@", string);

字符串中有特殊符號%怎么辦

NSInteger count =50;

//%是一個特殊符號 如果在NSString中用到%需要如下寫法

NSString*string = [NSStringstringWithFormat:@"%zd%%",count];

//輸出結果是: 50%

NSLog(@"%@", string);

字符串中有特殊符號"怎么辦

NSInteger count =50;

//"是一個特殊符號, 如果在NSString中用到"需要用\進行轉義

NSString *string= [NSString stringWithFormat:@"%zd\"",count];

//輸出結果是: 50"

NSLog(@"%@", string);

6.禁止鎖屏

默認情況下,當設備一段時間沒有觸控動作時,iOS會鎖住屏幕。但有一些應用是不需要鎖屏的,比如視頻播放器。

[UIApplication sharedApplication].idleTimerDisabled =YES;

或[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

7.Button禁止觸摸事件的2種方式

大家應該知道, 有很多需求是在規定內不允許點擊Button, 并且讓用戶知道這個按鈕是不可以點擊的,那我們應該這樣設置:

//會改變按鈕的狀態,顏色會變灰

button.enabled =NO;

但是又有一個需求是既不能點擊也不要改變Button顏色:

//保持按鈕原來的狀態,顏色不會變

button.userInteractionEnabled =NO;

8.設置圖片圓角

首先你是否是這么設置的:

//cornerRadius 設置為self.iconImage圖片寬度的一半(圓形圖片)

self.iconImage.layer.cornerRadius =20;

self.iconImage.layer.masksToBounds =YES;

或者是在xib&storyboard中點擊要設置圓角的圖片:

xib或者storyboard設置圓角.png


在此之后建議大家盡量不要這么設置, 因為使用圖層過量會有卡頓現象, 特別是弄圓角或者陰影會很卡, 如果設置圖片圓角我們一般用繪圖來做:

/** 設置圓形圖片(放到分類中使用) */

- (UIImage*)cutCircleImage {

? ? ? ? ?UIGraphicsBeginImageContextWithOptions(self.size,NO,0.0);

? ? ? ? // 獲取上下文

? ? ? ? ? CGContextRef ctr =UIGraphicsGetCurrentContext();

? ? ? ? ?// 設置圓形

? ? ? ? ? CGRect rect =CGRectMake(0,0,self.size.width,self.size.height);

? ? ? ? ? CGContextAddEllipseInRect(ctr, rect);

? ? ? ? ? // 裁剪 ? ? ?

? ? ? ? ? CGContextClip(ctr);

? ? ? ? ?// 將圖片畫上去

? ? ? ? ?[self drawInRect:rect];

? ? ? ? ? UIImage*image =UIGraphicsGetImageFromCurrentImageContext();

? ? ? ? ? UIGraphicsEndImageContext();

? ? ? ? ? return image;

}

這個方法就是設置圓角圖片, 效率很高, 不會造成卡頓現象, 大家要把這個方法單獨放到分類中使用

9.自動布局Autolayout口訣

在storyboard或者xib使用自動布局, 如果控件比較多而且布局復雜, 一不小心就會報一大堆錯誤警告, 那么這個口訣是必備良藥, 跟著這個口訣走再也不用害怕Autolayout恐懼癥了!

Autolayout.png

按照如上圖從上到下順序讀就是當前這條約束的狀態, 在xib中Constant與Multiplier不用區分順序問題, 通過網友(落水雨辰)的提醒, 如果在代碼中需要先Multiplier在Constant(蘋果官方的解釋):

First Item(登錄按鈕的頂部)Relation(等于)Second Item(父類 View的頂部)Multiplier(乘以 1)Constant(加上 10)

10.修改UITextField中Placeholder的文字顏色

[textField setValue:[UIColor redColor]forKeyPath:@"_placeholderLabel.textColor"];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容