转自OPEN经验

1,打印View所有子视图

po [[self view]recursiveDescription]

2,layoutSubviews调用的调用时机

* 当视图第一次显示的时候会被调用
* 当这个视图显示到屏幕上了,点击按钮
* 添加子视图也会调用这个方法
* 当本视图的大小发生改变的时候是会调用的
* 当子视图的frame发生改变的时候是会调用的
* 当删除子视图的时候是会调用的

3,NSString过滤特殊字符

// 定义一个特殊字符的集合
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:
@"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()_+'\""];
// 过滤字符串的特殊字符
NSString *newString = [trimString stringByTrimmingCharactersInSet:set];

4,TransForm属性

//平移按钮
CGAffineTransform transForm = self.buttonView.transform;
self.buttonView.transform = CGAffineTransformTranslate(transForm, 10, 0); //旋转按钮
CGAffineTransform transForm = self.buttonView.transform;
self.buttonView.transform = CGAffineTransformRotate(transForm, M_PI_4); //缩放按钮
self.buttonView.transform = CGAffineTransformScale(transForm, 1.2, 1.2); //初始化复位
self.buttonView.transform = CGAffineTransformIdentity;

5,去掉分割线多余15像素

首先在viewDidLoad方法加入以下代码:
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
然后在重写willDisplayCell方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}

6,计算方法耗时时间间隔

// 获取时间间隔
#define TICK CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#define TOCK NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)

7,Color颜色宏定义

// 随机颜色
#define RANDOM_COLOR [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1]
// 颜色(RGB)
#define RGBCOLOR(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define RGBACOLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

8,Alert提示宏定义

#define Alert(_S_, ...) [[[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:(_S_), ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show]

8,让 iOS 应用直接退出

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

8,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);

9,修改Label中不同文字颜色

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self editStringColor:self.label.text editStr:@"好" color:[UIColor blueColor]];
} - (void)editStringColor:(NSString *)string editStr:(NSString *)editStr color:(UIColor *)color {
// string为整体字符串, editStr为需要修改的字符串
NSRange range = [string rangeOfString:editStr]; NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:string]; // 设置属性修改字体颜色UIColor与大小UIFont
[attribute addAttributes:@{NSForegroundColorAttributeName:color} range:range]; self.label.attributedText = attribute;
}

10,播放声音

  #import<AVFoundation/AVFoundation.h>
// 1.获取音效资源的路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"pour_milk" ofType:@"wav"];
// 2.将路劲转化为url
NSURL *tempUrl = [NSURL fileURLWithPath:path];
// 3.用转化成的url创建一个播放器
NSError *error = nil;
AVAudioPlayer *play = [[AVAudioPlayer alloc]initWithContentsOfURL:tempUrl error:&error];
self.player = play;
// 4.播放
[play play];

11,检测是否IPad Pro

- (BOOL)isIpadPro
{
UIScreen *Screen = [UIScreen mainScreen];
CGFloat width = Screen.nativeBounds.size.width/Screen.nativeScale;
CGFloat height = Screen.nativeBounds.size.height/Screen.nativeScale;
BOOL isIpad =[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
BOOL hasIPadProWidth = fabs(width - 1024.f) < DBL_EPSILON;
BOOL hasIPadProHeight = fabs(height - 1366.f) < DBL_EPSILON;
return isIpad && hasIPadProHeight && hasIPadProWidth;
}

11,修改Tabbar Item的属性

    // 修改标题位置
self.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -10);
// 修改图片位置
self.tabBarItem.imageInsets = UIEdgeInsetsMake(-3, 0, 3, 0); // 批量修改属性
for (UIBarItem *item in self.tabBarController.tabBar.items) {
[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica" size:19.0], NSFontAttributeName, nil]
forState:UIControlStateNormal];
} // 设置选中和未选中字体颜色
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]]; //未选中字体颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]} forState:UIControlStateNormal]; //选中字体颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor cyanColor]} forState:UIControlStateSelected];

12,NULL - nil - Nil - NSNULL的区别

* nil是OC的,空对象,地址指向空(0)的对象。对象的字面零值

* Nil是Objective-C类的字面零值

* NULL是C的,空地址,地址的数值是0,是个长整数

* NSNull用于解决向NSArray和NSDictionary等集合中添加空值的问题

11,去掉BackBarButtonItem的文字

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];

12,控件不能交互的一些原因

1,控件的userInteractionEnabled = NO
2,透明度小于等于0.01,aplpha
3,控件被隐藏的时候,hidden = YES
4,子视图的位置超出了父视图的有效范围,子视图无法交互,设置了

12,修改UITextField中Placeholder的文字颜色

[text setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
}

13,视图的生命周期

``objc

1、 alloc 创建对象,分配空间

2、 init (initWithNibName) 初始化对象,初始化数据

3、 loadView 从nib载入视图 ,除非你没有使用xib文件创建视图

4、 viewDidLoad 载入完成,可以进行自定义数据以及动态创建其他控件

5、 viewWillAppear视图将出现在屏幕之前,马上这个视图就会被展现在屏幕上了

6、 viewDidAppear 视图已在屏幕上渲染完成

```objc
1、viewWillDisappear 视图将被从屏幕上移除之前执行
2、viewDidDisappear 视图已经被从屏幕上移除,用户看不到这个视图了
3、dealloc 视图被销毁,此处需要对你在init和viewDidLoad中创建的对象进行释放. viewVillUnload- 当内存过低,即将释放时调用;
viewDidUnload-当内存过低,释放一些不需要的视图时调用。

14,应用程序的生命周期

1,启动但还没进入状态保存 :
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions 2,基本完成程序准备开始运行:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 3,当应用程序将要入非活动状态执行,应用程序不接收消息或事件,比如来电话了:
- (void)applicationWillResignActive:(UIApplication *)application 4,当应用程序入活动状态执行,这个刚好跟上面那个方法相反:
- (void)applicationDidBecomeActive:(UIApplication *)application 5,当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可:
- (void)applicationDidEnterBackground:(UIApplication *)application 6,当程序从后台将要重新回到前台时候调用,这个刚好跟上面的那个方法相反:
- (void)applicationWillEnterForeground:(UIApplication *)application 7,当程序将要退出是被调用,通常是用来保存数据和一些退出前的清理工作:
- (void)applicationWillTerminate:(UIApplication *)application

15,判断view是不是指定视图的子视图

 BOOL isView =  [textView isDescendantOfView:self.view];

16,判断对象是否遵循了某协议

if ([self.selectedController conformsToProtocol:@protocol(RefreshPtotocol)]) {
[self.selectedController performSelector:@selector(onTriggerRefresh)];
}

17,页面强制横屏

#pragma mark - 强制横屏代码
- (BOOL)shouldAutorotate{
//是否支持转屏
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
//支持哪些转屏方向
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)prefersStatusBarHidden{
return NO;
}

18,系统键盘通知消息

1、UIKeyboardWillShowNotification-将要弹出键盘
2、UIKeyboardDidShowNotification-显示键盘
3、UIKeyboardWillHideNotification-将要隐藏键盘
4、UIKeyboardDidHideNotification-键盘已经隐藏
5、UIKeyboardWillChangeFrameNotification-键盘将要改变frame
6、UIKeyboardDidChangeFrameNotification-键盘已经改变frame

19,关闭navigationController的滑动返回手势

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

20,设置状态栏为任意的颜色

- (void)setStatusColor
{
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
statusBarView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:statusBarView];
}

21,让Xcode的控制台支持LLDB类型的打印

打开终端输入三条命令:
touch ~/.lldbinit
echo display @import UIKit >> ~/.lldbinit
echo target stop-hook add -o \"target stop-hook disable\" >> ~/.lldbinit 下次重新运行项目,然后就不报错了。 iOS 知识-常用小技巧大杂烩

22,Label行间距

-(void)test{
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:self.contentLabel.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:3]; //调整行间距
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [self.contentLabel.text length])];
self.contentLabel.attributedText = attributedString;
}

23,UIImageView填充模式

@"UIViewContentModeScaleToFill",      // 拉伸自适应填满整个视图
@"UIViewContentModeScaleAspectFit", // 自适应比例大小显示
@"UIViewContentModeScaleAspectFill", // 原始大小显示
@"UIViewContentModeRedraw", // 尺寸改变时重绘
@"UIViewContentModeCenter", // 中间
@"UIViewContentModeTop", // 顶部
@"UIViewContentModeBottom", // 底部
@"UIViewContentModeLeft", // 中间贴左
@"UIViewContentModeRight", // 中间贴右
@"UIViewContentModeTopLeft", // 贴左上
@"UIViewContentModeTopRight", // 贴右上
@"UIViewContentModeBottomLeft", // 贴左下
@"UIViewContentModeBottomRight", // 贴右下

24,宏定义检测block是否可用

#define BLOCK_EXEC(block, ...) if (block) { block(__VA_ARGS__); };
// 宏定义之前的用法
if (completionBlock) {
completionBlock(arg1, arg2);
}
// 宏定义之后的用法
BLOCK_EXEC(completionBlock, arg1, arg2);

文/品味_生活(简书)

via:http://www.jianshu.com/p/7c3ee5e67d03

iOS小知识点大杂烩的更多相关文章

  1. iOS 小知识点(持续更新)

    1.如何通过代码设置Button  title的字体大小 设置Button.titleLabel.font = [UIFont systemFontOfSize:<#(CGFloat)#> ...

  2. iOS小知识点

    1.子视图超出frame的部分不显示  view.clipsToBounds 设置为YES;   UIScrollview的clipsToBounds默认就是YES 2.UIImage 有一个属性叫s ...

  3. iOS小知识点(非UI部分)

    1. _cmd 表示当前方法的@SEL指针, - (void)putString{} 对于这个函数_cmd 等效于@selector(putString)

  4. iOS小知识点(UI部分)

    1. 父视图通过Tag来找到UIView UIView *targetView = [superView viewWithTag:10];//只在当前视图以及subviews中找,不能再孙子中找. 2 ...

  5. iOS小知识点记录

    1.创建视图的两种方法:用代码创建视图,创建XIB文件.如何决定使用哪种方法?参考法则:如果视图没有子视图,就用代码创建:如果有子视图,就通过XIB文件创建. 2.创建视图的时候,视图控制器会调用lo ...

  6. 刚接触Linux,菜鸟必备的小知识点(一)

    身为一个将要大四的学生,而且还是学计算机的没有接触过linux简直是羞愧难当.这个假期做了一个软件测试员,必须要熟悉linux的操作,所以对于我这个菜鸟我也就说几点比较重要的小知识点吧. 第一.cd指 ...

  7. Java学习过程中的总结的小知识点(长期更新)

    Java学习过程中的总结的小知识点 (主要是自己不会的知识和容易搞错的东西) 计算某个程序运行的时间 long stime=System.currentTimeMillis(); copy3(file ...

  8. 【转】HTML5的小知识点小集合

    html5的小知识点小集合 html5知识   1.  Doctype作用?标准模式与兼容模式各有什么区别? (1).<!DOCTYPE>声明位于位于HTML文档中的第一行,处于<h ...

  9. AngularJS的小知识点

    小知识点:$scope和$rootScope (1)每次使用ngController指令,都会调用控制器的创建函数,创建出一个控制器对象. (2)每次创建一个控制器对象,AngularJS都会创建一个 ...

随机推荐

  1. aix knowlgdgecenter

    http://www-01.ibm.com/support/knowledgecenter/ssw_aix_53/com.ibm.aix.install/doc/insgdrf/HT_insgdrf_ ...

  2. shell 变量自增(转)

    原文地址:http://www.cnblogs.com/iloveyoucc/archive/2012/07/11/2585559.html Linux Shell中写循环时,常常要用到变量的自增,现 ...

  3. 5.4const对象

    常成员函数 一个const对象可以调用const函数,但不能调用非const成员函数.必须将关键字const放在函数参数表之后,才能说明该函数是一个const成员函数. 声明常成员函数的格式如下: 类 ...

  4. checkbox和radio的样式美化问题

    如果你下定决心要改变现有的默认的checkbox和radio的样式,那么我目前有两种办法: 1.自己动手写一个,也就是自己写代码实现将input的checkbox和radio默认的样式隐藏掉,使用绝对 ...

  5. Egret及Node.js的安装部署

    最近在学Html5游戏开发,我选择的是国内的一个游戏开发框架egret.因为涉及到node.js这个近年来新兴起来的技术.借此机会把这方面知识学习一下. node.js以及egret的操作类似于Lin ...

  6. HighlightingSystem插件使用(边缘发光)

    插件链接: http://pan.baidu.com/s/1dFwkaTr 密码: nw2c 导入Unity里面可能会报错,不过没关系,直接注释掉就可以了,我用的是Unity5.1的版本 可以看到如下 ...

  7. 用正则表达式替换内容 php

    以前做一个项目,就是有一个问答的页面,比如说在回答或者提问的内容中插入表情.写到内容里的是表情图片的名字(而且是不带后缀的,比如:f_002.png)表情包放在项目里,我需要在取除内容的时候将里面的表 ...

  8. php Mysql 和Mysqli数据库函数整合

    PHP Mysql和Mysqli数据库函数整合 服务器如果支持mysqli函数将优先mysqli函数进行数据库操作 否则将调用mysql函数进行数据库操作 用法SQL::connect(host,us ...

  9. Android 调用系统通讯录

    这个过程有三步:1)许可向你的应用程序的manifest中添加读取通讯录数据的许可  <uses-permission android:name="android.permission ...

  10. Android StrictMode介绍

    转:http://www.blueowls.net/android-strictmode%E4%BB%8B%E7%BB%8D/ /** * enables "strict mode" ...