在iOS开发中经常会遇到一些看似非常复杂且不好理解,但知道方法后恍然大悟的代码片段,我进行了一下基本的整理归纳供大家学习交流,希望大家可以找到自习需要的东西,也欢迎补充,我也会在以后的的文章中陆续更新,下面开始撸代码!!!
1、改变 UITextField 占位文字 颜色
[_userName setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
2、禁止横屏 在Appdelegate 使用
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskPortrait;
}
3、修改状态栏颜色 (默认黑色,修改为白色)
//1.在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO
//2.在需要改变状态栏颜色的 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 {
//是否支持转屏
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//支持哪些转屏方向
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//屏幕的初始旋转方向
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)prefersStatusBarHidden {
return NO;
}
6、在状态栏显示有网络请求的提示器
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
7、相对路径
$(SRCROOT)/
8、视图是否自动(只是把第一个自动)向下挪64
self.automaticallyAdjustsScrollViewInsets = NO; //不让系统帮咱们把scrollView及其子类的视图向下调整64
9、 隐藏手机的状态栏
-(BOOL)prefersStatusBarHidden {
return YES;
}
10、代理的安全保护【断是否有代理,和代理是否执行了代理方法】
if (self.delegate && [self.delegate respondsToSelector:@selector(passValueWithArray:)]) {
// make you codes
}
11、在ARC工程中导入MRC的类和在MRC工程中导入ARC的类
// 在ARC工程中导入MRC的类 我们选中工程->选中targets中的工程,然后选中Build Phases->在导入的类后边加入标记 - fno-objc-arc
// 在MRC工程中导入ARC的类 路径与上面一致,在该类后面加上标记 -fobjc-arc
12、通过2D仿射函数实现小的动画效果(变大缩小) --可用于自定义pageControl中
[UIView animateWithDuration:0.3 animations:^{
imageView.transform = CGAffineTransformMakeScale(2, 2);
} completion:^(BOOL finished) {
imageView.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
13、查看系统所有字体
for (id familyName in [UIFont familyNames]) {
NSLog(@"%@", familyName);
for (id fontName in [UIFont fontNamesForFamilyName:familyName]) NSLog(@" %@", fontName);
}
14、判断一个字符串是否为数字
NSCharacterSet *notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([str rangeOfCharacterFromSet:notDigits].location == NSNotFound) {// 是数字
} else {// 不是数字
}
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、获取当前导航控制器下前一个控制器
- (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();