日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发零碎知识点

發(fā)布時間:2025/3/20 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发零碎知识点 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

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

suspend的英文意思有:暫停; 懸; 掛; 延緩;

二、帶有中文的URL處理。(非UTF-8處理,注意一下)

大概舉個例子,類似下面的URL,里面直接含有中文,可能導(dǎo)致播放不了,那么我們要處理一個這個URL,因為他太操蛋了,居然用中文。

http://static.tripbe.com/videofiles/視頻/我的自拍視頻.mp4 NSString *path = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)model.mp4_url, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));

三、獲取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); }

四、給UIView設(shè)置圖片(UILabel一樣適用)

  • 第一種方法:?
    利用的UIView的設(shè)置背景顏色方法,用圖片做圖案顏色,然后傳給背景顏色。

    UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"bgImg.png"];UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)]; [myView setBackGroundColor:bgColor];
  • 第二種方法:

    UIImage *image = [UIImage imageNamed:@"yourPicName@2x.png"]; yourView.layer.contents = (__bridge id)image.CGImage; //設(shè)置顯示的圖片范圍 yourView.layer.contentsCenter = CGRectMake(0.25,0.25,0.5,0.5);//四個值在0-1之間,對應(yīng)的為x,y,width,height。

五、去掉UITableView多余的分割線

yourTableView.tableFooterView = [UIView new];

六、調(diào)整cell分割線的位置,兩個方法一起用,暴力解決,防脫發(fā)

-(void)viewDidLayoutSubviews {if ([self.mytableview respondsToSelector:@selector(setSeparatorInset:)]) {[self.mytableview setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];}if ([self.mytableview respondsToSelector:@selector(setLayoutMargins:)]) {[self.mytableview setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];}}#pragma mark - cell分割線 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {if ([cell respondsToSelector:@selector(setSeparatorInset:)]){[cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];}if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {[cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];} }

七、UILabel和UIImageView的交互userInteractionEabled默認為NO。那么如果你把這兩個類做為父試圖的話,里面的所有東東都不可以點擊哦。曾經(jīng)有一個人,讓我?guī)兔φ{(diào)試bug,他調(diào)試很久沒搞定,就是把WMPlayer對象(播放器對象)放到一個UIImageView上面。這樣imageView addSubView:wmPlayer 后,播放器的任何東東都不能點擊了。userInteractionEabled設(shè)置為YES即可。

八、UISearchController和UISearchBar的Cancle按鈕改title問題,簡單粗暴

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {searchController.searchBar.showsCancelButton = YES;UIButton *canceLBtn = [searchController.searchBar valueForKey:@"cancelButton"];[canceLBtn setTitle:@"取消" forState:UIControlStateNormal];[canceLBtn setTitleColor:[UIColor colorWithRed:14.0/255.0 green:180.0/255.0 blue:0.0/255.0 alpha:1.00] forState:UIControlStateNormal];searchBar.showsCancelButton = YES;return YES; }

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

另外一個枚舉為UIScrollViewKeyboardDismissModeInteractive,表示在鍵盤內(nèi)部滑動,鍵盤逐漸下去。

十、NSTimer??
1、NSTimer計算的時間并不精確?
2、NSTimer需要添加到runLoop運行才會執(zhí)行,但是這個runLoop的線程必須是已經(jīng)開啟。?
3、NSTimer會對它的tagert進行retain,我們必須對其重復(fù)性的使用intvailte停止。target如果是self(指UIViewController),那么VC的retainCount+1,如果你不釋放NSTimer,那么你的VC就不會dealloc了,內(nèi)存泄漏了。

十一、UIViewController沒用大小(frame)?
經(jīng)常有人在群里問:怎么改變VC的大小啊??
瞬間無語。(只有UIView才能設(shè)置大小,VC是控制器啊,哥!)

十二、用十六進制獲取UIColor(類方法或者Category都可以,這里我用工具類方法)

+ (UIColor *)colorWithHexString:(NSString *)color {NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];// String should be 6 or 8 charactersif ([cString length] < 6) {return [UIColor clearColor];}// strip 0X if it appearsif ([cString hasPrefix:@"0X"])cString = [cString substringFromIndex:2];if ([cString hasPrefix:@"#"])cString = [cString substringFromIndex:1];if ([cString length] != 6)return [UIColor clearColor];// Separate into r, g, b substringsNSRange range;range.location = 0;range.length = 2;//rNSString *rString = [cString substringWithRange:range];//grange.location = 2;NSString *gString = [cString substringWithRange:range];//brange.location = 4;NSString *bString = [cString substringWithRange:range];// Scan valuesunsigned int r, g, b;[[NSScanner scannerWithString:rString] scanHexInt:&r];[[NSScanner scannerWithString:gString] scanHexInt:&g];[[NSScanner scannerWithString:bString] scanHexInt:&b];return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f]; }

十三、獲取今天是星期幾

+ (NSString *) getweekDayStringWithDate:(NSDate *) date {NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 指定日歷的算法NSDateComponents *comps = [calendar components:NSWeekdayCalendarUnit fromDate:date];// 1 是周日,2是周一 3.以此類推NSNumber * weekNumber = @([comps weekday]);NSInteger weekInt = [weekNumber integerValue];NSString *weekDayString = @"(周一)";switch (weekInt) {case 1:{weekDayString = @"(周日)";}break;case 2:{weekDayString = @"(周一)";}break;case 3:{weekDayString = @"(周二)";}break;case 4:{weekDayString = @"(周三)";}break;case 5:{weekDayString = @"(周四)";}break;case 6:{weekDayString = @"(周五)";}break;case 7:{weekDayString = @"(周六)";}break;default:break;}return weekDayString;}

十四、UIView的部分圓角問題

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)]; view2.backgroundColor = [UIColor redColor]; [self.view addSubview:view2];UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = view2.bounds; maskLayer.path = maskPath.CGPath; view2.layer.mask = maskLayer; //其中, byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight //指定了需要成為圓角的角。該參數(shù)是UIRectCorner類型的,可選的值有: * UIRectCornerTopLeft * UIRectCornerTopRight * UIRectCornerBottomLeft * UIRectCornerBottomRight * UIRectCornerAllCorners

從名字很容易看出來代表的意思,使用“|”來組合就好了。

十五、設(shè)置滑動的時候隱藏navigationBar

navigationController.hidesBarsOnSwipe = Yes;

十六、iOS畫虛線?
記得先 QuartzCore框架的導(dǎo)入

#import <QuartzCore/QuartzCore.h>CGContextRef context =UIGraphicsGetCurrentContext(); CGContextBeginPath(context); CGContextSetLineWidth(context, 2.0); CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); CGFloat lengths[] = {10,10}; CGContextSetLineDash(context, 0, lengths,2); CGContextMoveToPoint(context, 10.0, 20.0); CGContextAddLineToPoint(context, 310.0,20.0); CGContextStrokePath(context); CGContextClosePath(context);

十七、自動布局中多行UILabel,需要設(shè)置其preferredMaxLayoutWidth屬性才能正常顯示多行內(nèi)容。另外如果出現(xiàn)顯示不全文本,可以在計算的結(jié)果基礎(chǔ)上+0.5。

CGFloat h = [model.message boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - kGAP-kAvatar_Size - 2*kGAP, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height+0.5;

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

十九、KVC相關(guān),支持操作符?
KVC同時還提供了很復(fù)雜的函數(shù),主要有下面這些?
①簡單集合運算符?
簡單集合運算符共有@avg, @count , @max , @min ,@sum5種,都表示啥不用我說了吧, 目前還不支持自定義。

@interface Book : NSObject @property (nonatomic,copy) NSString* name; @property (nonatomic,assign) CGFloat price; @end @implementation Book @endBook *book1 = [Book new]; book1.name = @"The Great Gastby"; book1.price = 22; Book *book2 = [Book new]; book2.name = @"Time History"; book2.price = 12; Book *book3 = [Book new]; book3.name = @"Wrong Hole"; book3.price = 111;Book *book4 = [Book new]; book4.name = @"Wrong Hole"; book4.price = 111;NSArray* arrBooks = @[book1,book2,book3,book4]; NSNumber* sum = [arrBooks valueForKeyPath:@"@sum.price"]; NSLog(@"sum:%f",sum.floatValue); NSNumber* avg = [arrBooks valueForKeyPath:@"@avg.price"]; NSLog(@"avg:%f",avg.floatValue); NSNumber* count = [arrBooks valueForKeyPath:@"@count"]; NSLog(@"count:%f",count.floatValue); NSNumber* min = [arrBooks valueForKeyPath:@"@min.price"]; NSLog(@"min:%f",min.floatValue); NSNumber* max = [arrBooks valueForKeyPath:@"@max.price"]; NSLog(@"max:%f",max.floatValue);打印結(jié)果 2016-04-20 16:45:54.696 KVCDemo[1484:127089] sum:256.000000 2016-04-20 16:45:54.697 KVCDemo[1484:127089] avg:64.000000 2016-04-20 16:45:54.697 KVCDemo[1484:127089] count:4.000000 2016-04-20 16:45:54.697 KVCDemo[1484:127089] min:12.000000

NSArray 快速求總和 最大值 最小值 和 平均值

NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil]; CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue]; CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue]; CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue]; CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue]; NSLog(@"%f\n%f\n%f\n%f",sum,avg,max,min);

二十、使用MBProgressHud時,盡量不要加到UIWindow上,加self.view上即可。如果加UIWindow上在iPad上,旋轉(zhuǎn)屏幕的時候MBProgressHud不會旋轉(zhuǎn)。之前有人遇到這個bug,我讓他改放到self.view上即可解決此bug。

二十一、強制讓App直接退出(非閃退,非崩潰)

- (void)exitApplication {AppDelegate *app = [UIApplication sharedApplication].delegate;UIWindow *window = app.window;[UIView animateWithDuration:1.0f animations:^{window.alpha = 0;} completion:^(BOOL finished) {exit(0);}];}

二十二、Label行間距

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.contentLabel.text];NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:3];//調(diào)整行間距 [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.contentLabel.text length])];self.contentLabel.attributedText = attributedString;

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

二十四、MRC和ARC混編設(shè)置方式?
在XCode中targets的build phases選項下Compile Sources下選擇 不需要arc編譯的文件?
雙擊輸入 -fno-objc-arc 即可?
MRC工程中也可以使用ARC的類,方法如下:?
在XCode中targets的build phases選項下Compile Sources下選擇要使用arc編譯的文件?
雙擊輸入 -fobjc-arc 即可

二十五、把tableview里cell的小對勾的顏色改成別的顏色?
_yourTableView.tintColor = [UIColor redColor];

二十六、解決同時按兩個按鈕進兩個view的問題?
[button setExclusiveTouch:YES];

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

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

二十八、禁止textField和textView的復(fù)制粘貼菜單

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

二十九:如何進入我的軟件在app store 的頁面?
先用iTunes Link Maker找到軟件在訪問地址,格式為itms-apps://ax.itunes.apple.com/…,然后

#define ITUNESLINK @"itms-apps://ax.itunes.apple.com/..." NSURL *url = [NSURL URLWithString:ITUNESLINK]; if([[UIApplication sharedApplication] canOpenURL:url]){[[UIApplication sharedApplication] openURL:url]; }

如果把上述地址中itms-apps改為http就可以在瀏覽器中打開了。可以把這個地址放在自己的網(wǎng)站里,鏈接到app store。?
iTunes Link Maker地址:http://itunes.apple.com/linkmaker

三十、二級三級頁面隱藏系統(tǒng)tabbar?
1、單個處理

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

2.統(tǒng)一在基類里面處理?
新建一個類BaseNavigationController繼承UINavigationController,然后重寫 -(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated這個方法。所有的push事件都走此方法。

@interface BaseNavigationController : UINavigationController@end-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{[super pushViewController:viewController animated:animated];if (self.viewControllers.count>1) {viewController.hidesBottomBarWhenPushed = YES;}}

三十一、取消系統(tǒng)的返回手勢

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

三十二、修改UIWebView中字體的大小,顏色

1、UIWebView設(shè)置字體大小,顏色,字體:?
UIWebView無法通過自身的屬性設(shè)置字體的一些屬性,只能通過html代碼進行設(shè)置?
在webView加載完畢后,在?

- (void)webViewDidFinishLoad:(UIWebView *)webView方法中加入js代碼 NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '60%'"; [_webView stringByEvaluatingJavaScriptFromString:str];

或者加入以下代碼

NSString *jsString = [[NSString alloc] initWithFormat:@"document.body.style.fontSize=%f;document.body.style.color=%@",fontSize,fontColor]; [webView stringByEvaluatingJavaScriptFromString:jsString];

轉(zhuǎn)載于:https://www.cnblogs.com/tianxin418ios/p/5763869.html

總結(jié)

以上是生活随笔為你收集整理的iOS开发零碎知识点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。