在iOS開發(fā)中經(jīng)常會遇到一些看似非常復(fù)雜且不好理解,但知道方法后恍然大悟的代碼片段,我進(jìn)行了一下基本的整理歸納供大家學(xué)習(xí)交流,希望大家可以找到自習(xí)需要的東西,也歡迎補充,我也會在以后的的文章中陸續(xù)更新,下面開始擼代碼!!!
1、改變 UITextField 占位文字 顏色
[_userName setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
2、禁止橫屏 在Appdelegate 使用
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskPortrait;
}
3歪玲、修改狀態(tài)欄顏色 (默認(rèn)黑色唯鸭,修改為白色)
//1.在Info.plist中設(shè)置UIViewControllerBasedStatusBarAppearance 為NO
//2.在需要改變狀態(tài)欄顏色的 AppDelegate中在 didFinishLaunchingWithOptions 方法中增加:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
//3.如果需要在單個ViewController中添加河狐,在ViewDidLoad方法中增加:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
4郎哭、模糊效果
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *test = [[UIVisualEffectView alloc] initWithEffect:effect];
test.frame = self.view.bounds;
test.alpha = 0.5;
[self.view addSubview:test];
5狂鞋、強制橫屏代碼
#pragma mark - 強制橫屏代碼
- (BOOL)shouldAutorotate {
//是否支持轉(zhuǎn)屏
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//支持哪些轉(zhuǎn)屏方向
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//屏幕的初始旋轉(zhuǎn)方向
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)prefersStatusBarHidden {
return NO;
}
6著淆、在狀態(tài)欄顯示有網(wǎng)絡(luò)請求的提示器
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
7劫狠、相對路徑
$(SRCROOT)/
8、視圖是否自動(只是把第一個自動)向下挪64
self.automaticallyAdjustsScrollViewInsets = NO; //不讓系統(tǒng)幫咱們把scrollView及其子類的視圖向下調(diào)整64
9永部、 隱藏手機的狀態(tài)欄
-(BOOL)prefersStatusBarHidden {
return YES;
}
10独泞、代理的安全保護(hù)【斷是否有代理,和代理是否執(zhí)行了代理方法】
if (self.delegate && [self.delegate respondsToSelector:@selector(passValueWithArray:)]) {
// make you codes
}
11苔埋、在ARC工程中導(dǎo)入MRC的類和在MRC工程中導(dǎo)入ARC的類
// 在ARC工程中導(dǎo)入MRC的類 我們選中工程->選中targets中的工程,然后選中Build Phases->在導(dǎo)入的類后邊加入標(biāo)記 - fno-objc-arc
// 在MRC工程中導(dǎo)入ARC的類 路徑與上面一致,在該類后面加上標(biāo)記 -fobjc-arc
12懦砂、通過2D仿射函數(shù)實現(xiàn)小的動畫效果(變大縮小) --可用于自定義pageControl中
[UIView animateWithDuration:0.3 animations:^{
imageView.transform = CGAffineTransformMakeScale(2, 2);
} completion:^(BOOL finished) {
imageView.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
13、查看系統(tǒng)所有字體
for (id familyName in [UIFont familyNames]) {
NSLog(@"%@", familyName);
for (id fontName in [UIFont fontNamesForFamilyName:familyName]) NSLog(@" %@", fontName);
}
14、判斷一個字符串是否為數(shù)字
NSCharacterSet *notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([str rangeOfCharacterFromSet:notDigits].location == NSNotFound) {// 是數(shù)字
} else {// 不是數(shù)字
}
15荞膘、將一個view保存為pdf格式
- (void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[aView.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}
16罚随、讓一個view在父視圖中心
child.center = [parent convertPoint:parent.center fromView:parent.superview];
17、獲取當(dāng)前導(dǎo)航控制器下前一個控制器
- (UIViewController *)backViewController
{
NSInteger myIndex = [self.navigationController.viewControllers indexOfObject:self];
if ( myIndex != 0 && myIndex != NSNotFound ) {
return [self.navigationController.viewControllers objectAtIndex:myIndex-1];
} else {
return nil;
}
}
18羽资、保存UIImage到本地
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
19淘菩、鍵盤上方增加工具欄
UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered target:self
action:@selector(doneClicked:)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
txtField.inputAccessoryView = keyboardDoneButtonView;
20、在image上繪制文字并生成新的image
UIFont *font = [UIFont boldSystemFontOfSize:12];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[[UIColor whiteColor] set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();