生活随笔
收集整理的這篇文章主要介紹了
iOS之常用的方法和技巧
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
- (BOOL)isChineseFirst:(NSString *)firstString {//是否以中文開(kāi)頭(unicode中文編碼范圍是0x4e00~0x9fa5)int utfCode = 0;void *buffer = &utfCode;NSRange range = NSMakeRange(0, 1);//判斷是不是中文開(kāi)頭的,buffer->獲取字符的字節(jié)數(shù)據(jù) maxLength->buffer的最大長(zhǎng)度 usedLength->實(shí)際寫入的長(zhǎng)度,不需要的話可以傳遞NULL encoding->字符編碼常數(shù),不同編碼方式轉(zhuǎn)換后的字節(jié)長(zhǎng)是不一樣的,這里我用了UTF16 Little-Endian,maxLength為2字節(jié),如果使用Unicode,則需要4字節(jié) options->編碼轉(zhuǎn)換的選項(xiàng),有兩個(gè)值,分別是NSStringEncodingConversionAllowLossy和NSStringEncodingConversionExternalRepresentation range->獲取的字符串中的字符范圍,這里設(shè)置的第一個(gè)字符 remainingRange->建議獲取的范圍,可以傳遞NULLBOOL isChinese = [firstString getBytes:buffer maxLength:2 usedLength:NULL encoding:NSUTF16LittleEndianStringEncoding options:NSStringEncodingConversionExternalRepresentation range:range remainingRange:NULL];if (isChinese && (utfCode >= 0x4e00 && utfCode <= 0x9fa5))return YES;elsereturn NO;
}
- (void)setUpForDismissKeyboard {UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAnywhereToDismissKeyboard:)];NSOperationQueue *operation = [NSOperationQueue mainQueue];[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:operation usingBlock:^(NSNotification * _Nonnull note) {[self.view addGestureRecognizer:tapGesture];}];[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:operation usingBlock:^(NSNotification * _Nonnull note) {[self.view removeGestureRecognizer:tapGesture];}];
}
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {self.navigationController.interactivePopGestureRecognizer.enabled = NO;}
- (void)hiddenNavLine {if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){NSArray *list=self.navigationController.navigationBar.subviews;for (id obj in list) {if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {UIView *view = (UIView*)obj;for (id obj2 in view.subviews) {if ([obj2 isKindOfClass:[UIImageView class]]) {UIImageView *image = (UIImageView*)obj2;image.hidden = YES;}}}}}
}
- (void)removeSearchBarBackGroundView {for(int i = 0 ;i < _searchBar.subviews.count;i++){UIView * backView = _searchBar.subviews[i];if ([backView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {[backView removeFromSuperview];[_searchBar setBackgroundColor:[UIColor clearColor]];break;} else {NSArray * arr = _searchBar.subviews[i].subviews;for(int j = 0;j < arr.count; j++){UIView * barView = arr[i];if ([barView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {[barView removeFromSuperview];[_searchBar setBackgroundColor:[UIColor clearColor]];break;}}}}
}
- 標(biāo)簽欄的背景圖顯示錯(cuò)亂問(wèn)題解決辦法:
//將圖片如下設(shè)置
[[UIImage imageNamed:@"me@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
- 網(wǎng)絡(luò)請(qǐng)求設(shè)置超時(shí)請(qǐng)求(基于AFNetworking3.0封裝的GET,POST請(qǐng)求用方法)
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];manager.requestSerializer.timeoutInterval = 15.f;[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
- 實(shí)用的獲取圖片的方法,防止內(nèi)存溢出:
// 建議使用該方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"圖片" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];// 不建議使用該方法:
UIImage *image = [UIImage imageNamed:@"圖片.png"];
總結(jié)
以上是生活随笔為你收集整理的iOS之常用的方法和技巧的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。