iOS 開發小知識點

一、調用代碼使APP進入后臺,達到點擊Home鍵的效果。(私有API)

[[UIApplication sharedApplication] performSelector:@selector(suspend)];

suspend的英文意思:懸、掛、暫停

二、獲取UIWebView的高度 個人

(void)webViewDidFinishLoad:(UIWebView *)webView? {? ? ? CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];? ? ? CGRect frame = webView.frame;? ? ? webView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height);? }

三、UITableView收起鍵盤何必這么麻煩 一個屬性搞定,效果好(UIScrollView同樣可以使用) 以前是不是覺得[self.view endEditing:YES];很屌,這個下面的更屌。 yourTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

四、設置UILable ?的行間距 ?和 計算帶行間距的高度

/*

UILabel* 展示的控件

(NSString*)str? 內容

withFont:(float)font 字體大小

WithSpace:(float)space 行間距

*/

//給UILabel設置行間距和字間距

-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(float)font WithSpace:(float)space{

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

paraStyle.lineSpacing = space; //設置行間距

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

//設置字間距 NSKernAttributeName:@1.5f

UIFont *tfont = [UIFont systemFontOfSize:font];

NSDictionary *dic = @{NSFontAttributeName:tfont, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

};

NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];

label.attributedText = attributeStr;

}

/*

計算UILabel的高度(帶有行間距的情況)

(NSString*)str? 內容

withFont:(float)font 字體大小

WithSpace:(float)space 行間距

(CGFloat)width? UILable的寬度

*/

//計算UILabel的高度(帶有行間距的情況)

-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(float)font withWidth:(CGFloat)width WithSpace:(float)space{

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

paraStyle.lineSpacing = space;

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

UIFont *tfont = [UIFont systemFontOfSize:font];

NSDictionary *dic = @{NSFontAttributeName:tfont, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

};

CGSize size = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

return size.height;

}

五、 禁止程序運行時自動鎖屏 [[UIApplication sharedApplication] setIdleTimerDisabled:YES];

六、CocoaPods pod install/pod update更新慢的問題 pod install –verbose –no-repo-update pod update –verbose –no-repo-update 如果不加后面的參數,默認會升級CocoaPods的spec倉庫,加一個參數可以省略這一步,然后速度就會提升不少。

七、修改textFieldplaceholder字體顏色和大小

textField.placeholder = @"請輸入用戶名";? [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];? [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

八、禁止textField和textView的復制粘貼菜單

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{? ? if ([UIMenuController sharedMenuController]) {? ? ? [UIMenuController sharedMenuController].menuVisible = NO;? ? }? ? return NO;}

九、級三級頁面隱藏系統tabbar 1、單個處理

YourViewController *yourVC = [YourViewController new];yourVC.hidesBottomBarWhenPushed = YES;[self.navigationController pushViewController:yourVC animated:YES];

十、取消系統的返回手勢

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

十一、百度坐標跟火星坐標相互轉換

//百度轉火星坐標

+ (CLLocationCoordinate2D )bdToGGEncrypt:(CLLocationCoordinate2D)coord

{

double x = coord.longitude - 0.0065, y = coord.latitude - 0.006;

double z = sqrt(x * x + y * y) - 0.00002 * sin(y * M_PI);

double theta = atan2(y, x) - 0.000003 * cos(x * M_PI);

CLLocationCoordinate2D transformLocation ;

transformLocation.longitude = z * cos(theta);

transformLocation.latitude = z * sin(theta);

return transformLocation;

}

//火星坐標轉百度坐標

+ (CLLocationCoordinate2D )ggToBDEncrypt:(CLLocationCoordinate2D)coord

{

double x = coord.longitude, y = coord.latitude;

double z = sqrt(x * x + y * y) + 0.00002 * sin(y * M_PI);

double theta = atan2(y, x) + 0.000003 * cos(x * M_PI);

CLLocationCoordinate2D transformLocation ;

transformLocation.longitude = z * cos(theta) + 0.0065;

transformLocation.latitude = z * sin(theta) + 0.006;

return transformLocation;

}

十二、添加pch文件的步聚

1:創建新文件 ios->other->PCH file,創建一個pch文件:“工程名-Prefix.pch”:

2:將building setting中的precompile header選項的路徑添加“$(SRCROOT)/項目名稱/pch文件名”(例如:$(SRCROOT)/LotteryFive/LotteryFive-Prefix.pch)

3:將Precompile Prefix Header為YES,預編譯后的pch文件會被緩存起來,可以提高編譯速度

十三、關于Masonry

a:make.equalTo 或 make.greaterThanOrEqualTo (至多) 或 make.lessThanOrEqualTo(至少)

make.left.greaterThanOrEqualTo(label);

make.left.greaterThanOrEqualTo(label.mas_left);

//width >= 200 && width <= 400

make.width.greaterThanOrEqualTo(@200);

make.width.lessThanOrEqualTo(@400)

b:masequalTo 和 equalTo 區別:masequalTo 比equalTo多了類型轉換操作,一般來說,大多數時候兩個方法都是 通用的,但是對于數值元素使用mas_equalTo。對于對象或是多個屬性的處理,使用equalTo。特別是多個屬性時,必須使用equalTo

c:一些簡便賦值

// make top = superview.top + 5, left = superview.left + 10,

// bottom = superview.bottom - 15, right = superview.right - 20

make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20))

// make width and height greater than or equal to titleLabel

make.size.greaterThanOrEqualTo(titleLabel)

// make width = superview.width + 100, height = superview.height - 50

make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))

// make centerX = superview.centerX - 5, centerY = superview.centerY + 10

make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))

d:and關鍵字運用

make.left.right.and.bottom.equalTo(superview);

make.top.equalTo(otherView);

e:優先;優先權(.priority,.priorityHigh,.priorityMedium,.priorityLow)

.priority允許您指定一個確切的優先級

.priorityHigh 等價于UILayoutPriorityDefaultHigh

.priorityMedium 介于高跟低之間

.priorityLow 等價于UILayoutPriorityDefaultLow

實例:

make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();

make.top.equalTo(label.mas_top).with.priority(600);

g:使用mas_makeConstraints創建constraint后,你可以使用局部變量或屬性來保存以便下次引用它;如果創建多個constraints,你可以采用數組來保存它們

// 局部或者全局

@property (nonatomic, strong) MASConstraint *topConstraint;

// 創建約束并賦值

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {

self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);

make.left.equalTo(superview.mas_left).with.offset(padding.left);

}];

// 過后可以直接訪問self.topConstraint

[self.topConstraint uninstall];

h:mas_updateConstraints更新約束,有時你需要更新constraint(例如,動畫和調試)而不是創建固定constraint,可以使用mas_updateConstraints方法

- (void)updateConstraints {

[self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {

make.center.equalTo(self);

make.width.equalTo(@(self.buttonSize.width)).priorityLow();

make.height.equalTo(@(self.buttonSize.height)).priorityLow();

make.width.lessThanOrEqualTo(self);

make.height.lessThanOrEqualTo(self);

}];

//調用父updateConstraints

[super updateConstraints];

}

i:mas_remakeConstraints更新約束,mas_remakeConstraints與mas_updateConstraints比較相似,都是更新constraint。不過,mas_remakeConstraints是刪除之前constraint,然后再添加新的constraint(適用于移動動畫);而mas_updateConstraints只是更新constraint的值。

- (void)changeButtonPosition {

[self.button mas_remakeConstraints:^(MASConstraintMaker *make) {

make.size.equalTo(self.buttonSize);

if (topLeft) {

make.top.and.left.offset(10);

} else {

make.bottom.and.right.offset(-10);

}

}];

}

十四、UIWebView在IOS9下底部出現黑邊解決方式

UIWebView底部的黑條很難看(在IOS8下不會,在IOS9會出現),特別是在底部還有透明控件的時候,隱藏的做法其實很簡單,只需要將opaque設為NO,背景色設為clearColor即可

十五、數組逆序遍歷

1、枚舉法

NSArray *array = @[@"1",@"2",@"3",@"5",@"6"];

[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id? _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

NSLog(@"%@",obj);

}];

2、for循環

NSArray*array=@[@"1",@"2",@"3",@"5",@"6"];

for (NSInteger index = array.count-1; index>=0; index--) {

NSLog(@"%@",array[index]);

}

十六、把時間字符串2015-04-10格式化日期轉為NSDate類型

參考答案:

NSString *timeStr = @"2015-04-10";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

formatter.dateFormat = @"yyyy-MM-dd";

formatter.timeZone = [NSTimeZone defaultTimeZone];

NSDate *date = [formatter dateFromString:timeStr];

// 2015-04-09 16:00:00 +0000

NSLog(@"%@", date);

十七 、簡單寫出增、刪、改、查的SQL語句。

參考答案:

數據庫的簡單操作,還是會的,大學可沒白學。

增:

insert into tb_blogs(name, url) values('aaa','http://www.baidu.com');

刪:

delete from tb_blogs where blogid = 1;

改:

update tb_blogs set url = 'www.baidu.com' where blogid = 1;

查:

select name, url from tb_blogs where blogid = 1;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,156評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,401評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,069評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,873評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,635評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,128評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,203評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,365評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,881評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,733評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,935評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,475評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,172評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,582評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,821評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,595評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,908評論 2 372

推薦閱讀更多精彩內容