1,打印View所有子视图

  1. po [[self view]recursiveDescription]

2,layoutSubviews调用的调用时机

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

3,NSString过滤特殊字符

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

4,TransForm属性

  1. //平移按钮
  2. CGAffineTransform transForm = self.buttonView.transform;
  3. self.buttonView.transform = CGAffineTransformTranslate(transForm, 10, 0);
  4. //旋转按钮
  5. CGAffineTransform transForm = self.buttonView.transform;
  6. self.buttonView.transform = CGAffineTransformRotate(transForm, M_PI_4);
  7. //缩放按钮
  8. self.buttonView.transform = CGAffineTransformScale(transForm, 1.2, 1.2);
  9. //初始化复位
  10. self.buttonView.transform = CGAffineTransformIdentity;

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

  1. 首先在viewDidLoad方法加入以下代码:
  2. if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
  3. [self.tableView setSeparatorInset:UIEdgeInsetsZero];
  4. }
  5. if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
  6. [self.tableView setLayoutMargins:UIEdgeInsetsZero];
  7. }
  8. 然后在重写willDisplayCell方法
  9. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
  10. forRowAtIndexPath:(NSIndexPath *)indexPath{
  11. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  12. [cell setSeparatorInset:UIEdgeInsetsZero];
  13. }
  14. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  15. [cell setLayoutMargins:UIEdgeInsetsZero];
  16. }
  17. }

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

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

7,Color颜色宏定义

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

8,Alert提示宏定义

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

8,让 iOS 应用直接退出

  1. - (void)exitApplication {
  2. AppDelegate *app = [UIApplication sharedApplication].delegate;
  3. UIWindow *window = app.window;
  4. [UIView animateWithDuration:1.0f animations:^{
  5. window.alpha = 0;
  6. } completion:^(BOOL finished) {
  7. exit(0);
  8. }];
  9. }

8,NSArray 快速求总和 最大值 最小值 和 平均值

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

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

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

10,播放声音

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

11,检测是否IPad Pro

  1. - (BOOL)isIpadPro
  2. {
  3. UIScreen *Screen = [UIScreen mainScreen];
  4. CGFloat width = Screen.nativeBounds.size.width/Screen.nativeScale;
  5. CGFloat height = Screen.nativeBounds.size.height/Screen.nativeScale;
  6. BOOL isIpad =[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
  7. BOOL hasIPadProWidth = fabs(width - 1024.f) < DBL_EPSILON;
  8. BOOL hasIPadProHeight = fabs(height - 1366.f) < DBL_EPSILON;
  9. return isIpad && hasIPadProHeight && hasIPadProWidth;
  10. }

11,修改Tabbar Item的属性

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

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

  1. * nilOC的,空对象,地址指向空(0)的对象。对象的字面零值
  2. * NilObjective-C类的字面零值
  3. * NULLC的,空地址,地址的数值是0,是个长整数
  4. * NSNull用于解决向NSArrayNSDictionary等集合中添加空值的问题

11,去掉BackBarButtonItem的文字

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

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

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

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

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

13,视图的生命周期

  1. 1 alloc 创建对象,分配空间
  2. 2 init (initWithNibName) 初始化对象,初始化数据
  3. 3 loadView nib载入视图 ,除非你没有使用xib文件创建视图
  4. 4 viewDidLoad 载入完成,可以进行自定义数据以及动态创建其他控件
  5. 5 viewWillAppear视图将出现在屏幕之前,马上这个视图就会被展现在屏幕上了
  6. 6 viewDidAppear 视图已在屏幕上渲染完成
  7. 1viewWillDisappear 视图将被从屏幕上移除之前执行
  8. 2viewDidDisappear 视图已经被从屏幕上移除,用户看不到这个视图了
  9. 3dealloc 视图被销毁,此处需要对你在initviewDidLoad中创建的对象进行释放.
  10. viewVillUnload 当内存过低,即将释放时调用;
  11. viewDidUnload-当内存过低,释放一些不需要的视图时调用。

14,应用程序的生命周期

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

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

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

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

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

17,页面强制横屏

  1. #pragma mark - 强制横屏代码
  2. - (BOOL)shouldAutorotate{
  3. //是否支持转屏
  4. return NO;
  5. }
  6. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
  7. //支持哪些转屏方向
  8. return UIInterfaceOrientationMaskLandscape;
  9. }
  10. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
  11. return UIInterfaceOrientationLandscapeRight;
  12. }
  13. - (BOOL)prefersStatusBarHidden{
  14. return NO;
  15. }

18,系统键盘通知消息

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

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

  1. self.navigationController.interactivePopGestureRecognizer.enabled = NO;

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

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

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

  1. 打开终端输入三条命令:
  2. touch ~/.lldbinit
  3. echo display @import UIKit >> ~/.lldbinit
  4. echo target stop-hook add -o \"target stop-hook disable\" >> ~/.lldbinit

下次重新运行项目,然后就不报错了。

22,Label行间距

  1. -(voidtest{
  2. NSMutableAttributedString *attributedString =
  3. [[NSMutableAttributedString alloc] initWithString:self.contentLabel.text];
  4. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  5. [paragraphStyle setLineSpacing:3];
  6. //调整行间距
  7. [attributedString addAttribute:NSParagraphStyleAttributeName
  8. value:paragraphStyle
  9. range:NSMakeRange(0, [self.contentLabel.text length])];
  10. self.contentLabel.attributedText = attributedString;
  11. }

23,UIImageView填充模式

  1. @"UIViewContentModeScaleToFill", // 拉伸自适应填满整个视图
  2. @"UIViewContentModeScaleAspectFit", // 自适应比例大小显示
  3. @"UIViewContentModeScaleAspectFill", // 原始大小显示
  4. @"UIViewContentModeRedraw", // 尺寸改变时重绘
  5. @"UIViewContentModeCenter", // 中间
  6. @"UIViewContentModeTop", // 顶部
  7. @"UIViewContentModeBottom", // 底部
  8. @"UIViewContentModeLeft", // 中间贴左
  9. @"UIViewContentModeRight", // 中间贴右
  10. @"UIViewContentModeTopLeft", // 贴左上
  11. @"UIViewContentModeTopRight", // 贴右上
  12. @"UIViewContentModeBottomLeft", // 贴左下
  13. @"UIViewContentModeBottomRight", // 贴右下

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

  1. #define BLOCK_EXEC(block, ...) if (block) { block(__VA_ARGS__); };
  2. // 宏定义之前的用法
  3. if (completionBlock) {
  4. completionBlock(arg1, arg2);
  5. }
  6. // 宏定义之后的用法
  7. BLOCK_EXEC(completionBlock, arg1, arg2);

iOS开发技巧-2的更多相关文章

  1. iOS开发技巧系列---详解KVC(我告诉你KVC的一切)

    KVC(Key-value coding)键值编码,单看这个名字可能不太好理解.其实翻译一下就很简单了,就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值.而不需 ...

  2. 【转】几点 iOS 开发技巧

    [译] 几点 iOS 开发技巧 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高的 Mug ...

  3. 几点iOS开发技巧

    转自I'm Allen的博客   原文:iOS Programming Architecture and Design Guidelines   原文来自破船的分享   原文作者是开发界中知晓度相当高 ...

  4. iOS开发技巧

    一.寻找最近公共View 我们将一个路径中的所有点先放进 NSSet 中.因为 NSSet 的内部实现是一个 hash 表,所以查找元素的时间复杂度变成了 O(1),我们一共有 N 个节点,所以总时间 ...

  5. iOS开发技巧系列---使用链式编程和Block来实现UIAlertView

    UIAlertView是iOS开发过程中最常用的控件之一,是提醒用户做出选择最主要的工具.在iOS8及后来的系统中,苹果更推荐使用UIAlertController来代替UIAlertView.所以本 ...

  6. iOS开发技巧 -- 复用代码片段

    如果你是一位开发人员在开发过程中会发现有些代码无论是在同一个工程中还是在不同工程中使用率会很高,有经验的人会直接封装在一个类里,或者写成一个宏定义或者把这些代码收集起来,下次直接使用,或者放到xcod ...

  7. iOS开发技巧 - Size Class与iOS 8多屏幕适配(一)

    0. 背景: 在iOS开发中,Auto Layout(自动布局)能解决大部分的屏幕适配问题. 但是当iPhone 6和iPhone 6 Plus发布以后, Auto Layout已经不能解决复杂的屏幕 ...

  8. iOS 开发技巧收藏贴 链接整理

    54个技巧 57个技巧 正则表达式

  9. IOS开发技巧快速生成二维码

    随着移动互联网的发展,二维码应用非常普遍,各大商场,饭店,水果店 基本都有二维码的身影,那么ios中怎么生成二维码呢? 下面的的程序演示了快速生成二维码的方法: 在ios里面要生成二维码,需要借助一个 ...

随机推荐

  1. Java开发基础

    天数 课程 01 Java基础回顾 集合 泛型 IO流 多线程 Junit Properties   HTML   JavaScript   JavaScript   BOM编程   XML基础   ...

  2. MFC编程入门之二十四(常用控件:列表框控件ListBox)

    前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...

  3. 转:Eclipse插件开发之TreeViewer

    http://www.tuicool.com/articles/e6fmE3R contentprovider在插件开发和RCP(Rich Client Platform)开发中常常被用到,譬如你要创 ...

  4. [官方作品] 关于ES4的设首页问题

    [官方作品] 关于ES4的设首页问题 Skyfree 发表于 2013-2-10 21:55:03 https://www.itsk.com/thread-254503-1-1.html 关于ES4设 ...

  5. Linux基础知识

    1.url中不写端口号,默认就是80端口:本机是127.0.0.1或者localhost 2.用户管理 查看当前用户: id:可以查看当前用户:whoami:查看当前的用户:who:可以查看当前已经登 ...

  6. Oracle 表连接

    Oracle 表之间的连接分为三种: 1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制)      (2)右外连接(右边的表不加限制)      (3)全外连接(左右两表都不 ...

  7. Oculus安装问题

    1.必须FQ,可采用蓝灯,或其他vpn 2.Your computer doesn't meet Rift's recommended specifications 如果某些硬件达不到推荐配置(比如我 ...

  8. SPSS数据分析-时间序列模型

    我们在分析数据时,经常会碰到一种数据,它是由时间累积起来的,并按照时间顺序排列的一系列观测值,我们称为时间序列,它有点类似于重复测量数据,但是区别在于重复测量数据的时间点不会很多,而时间序列的时间点非 ...

  9. 数字图像处理作业使用OpenCV - 配置

    使用环境:Windows7 旗舰版 + vs2008 + OpenCV2.0a 基本上配置都是通过网上一个教程,在此附上地址 Click ME. 为了避免因不同版本而出现的安装问题,我还是下载了2.0 ...

  10. 初始JavaScript

    本文是笔者在看廖雪峰老师的JavaScript教程时的总结 一.加载 JavaScript           1.直接在html语句中写入JavaScript语句           2.在html ...