美文网首页iOS 开发iOS Developer将来跳槽用
【iOS 开发】常用方法和常见问题解决

【iOS 开发】常用方法和常见问题解决

作者: 爱吃鸭梨的猫 | 来源:发表于2017-03-20 12:29 被阅读35次
Xcode

这篇文章是写给自己看的,会将平时一些用得到但又不是特别常用的方法记录在这里,因为只是一些小方法,不至于每个都开一篇文章,所以就汇总在这里,有需要的朋友也可以收藏一下。


读取文件路径

/* 获取文件 URL 路径 */
NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"png"];
    
/* 获取文件 path 路径 */
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];
    
/* 获取 App 沙盒路径 */
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

按比例缩放尺寸来适配不同尺寸的屏幕

/* 得到屏幕尺寸 */
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
  
/* 设置比例缩放 */
#define W(x) ((x)* kScreenWidth / 320.0)
#define H(y) ((y)* kScreenHeight / 568.0)

延迟执行的方法

以下三种方法效果相同,都是在三秒延迟后设置 self.label.text = @"标题";

/* 方式一(在当前线程 但不会卡住界面) */
[self.label performSelector:@selector(setText:) withObject:@"标题" afterDelay:3];
/* 方式二(在当前线程 但不会卡住界面) */
[self performSelector:@selector(set) withObject:nil afterDelay:3];
  
- (void)set {
  
    self.label.text = @"标题";
}
/* 方式三(在其他线程) */
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
  
    self.label.text = @"标题";
});
/* 在当前线程 会卡住界面(一般只用作调试) */
[NSThread sleepForTimeInterval:3]; => sleep(3);

转换网址中的中文

/* 用于 GET 方式请求时,参数含有中文时的解决方法 */
NSString *str1 = @"http://apis.baidu.com/apistore/weatherservice/cityname=无锡";
  
NSString *str2 = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

根据类名创建视图控制器对象

UIViewController *viewController = [[NSClassFromString(vcName) alloc] init];

与用户的交互状态

/* 取消与用户的交互,界面上的控件都会失效(也可单独设置某控件,与 enabled 属性有区别) */
self.view.userInteractionEnabled = NO;

将控件显示在视图最前端

/* 会将该子视图放在所有子视图的最前面 */
[self.view bringSubviewToFront:self.button];

图片添加点击事件

有些人给图片添加点击事件时,会发现根本无效,其原因是因为 UIImageViewuserInteractionEnabled 属性默认是为 NO 的,所以添加了点击事件后,点击却没反应。

/* 图片想要点击事件必须设为YES,默认是NO */
self.imageView.userInteractionEnabled = YES;

SearchBar 或者 TextField 设置第一响应者

/* 就是进入页面直接进入编辑状态 */
[self.searchBar becomeFirstResponder];

自定义 Log 打印

/* 自定义 Log 打印 */
#ifdef DEBUG
#define DString [NSString stringWithFormat:@"%s", __FILE__].lastPathComponent
#define DLog(fmt, ...) NSLog((@"[%@ : %d 行] " fmt), DString, __LINE__, ##__VA_ARGS__)
#else
#define DLog(...)
#endif

获取当前设备系统版本号

/* 当前设备系统版本号 */
#define kDeviceSystemVersion [[UIDevice currentDevice].systemVersion doubleValue]

颜色转换为图片

/**
 颜色转换为图片
  
 @param color 要转换的颜色
 @return 转换后的图片
 */
+ (UIImage *)imageWithColor:(UIColor *)color {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

通过 storyboard 的 Identity 来查找对象

设置 Storyboard ID
/* 需设置要查找视图的ID */
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tab = [storyboard instantiateViewControllerWithIdentifier:@"tab"];
  
/* 根据根视图查找 */
UITabBarController *tab = storyboard.instantiateInitialViewController;

全局引用头文件

$(SRCROOT)/2048/define.h 是相对路径,表示 2048 项目文件夹下的 define.h 文件。

Prefix Header

解决 iOS11 中 ScrollView 顶部白边和 TableView 刷新抖动的问题

// 解决 iOS11 中 ScrollView 顶部白边和 TableView 刷新抖动的问题
if (@available(iOS 11.0, *)) {
    UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    UITableView.appearance.estimatedRowHeight = 0;
    UITableView.appearance.estimatedSectionHeaderHeight = 0;
    UITableView.appearance.estimatedSectionFooterHeight = 0;
}

将来的你,一定会感激现在拼命的自己,愿自己与读者的开发之路无限美好。

我的传送门: 博客简书微博GitHub

相关文章

网友评论

    本文标题:【iOS 开发】常用方法和常见问题解决

    本文链接:https://www.haomeiwen.com/subject/eoienttx.html